Avatar of JaeHyoLee

JaeHyoLee's solution

to House in the Lua Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Recite the nursery rhyme 'This is the House that Jack Built'.

[The] process of placing a phrase of clause within another phrase of clause is called embedding. It is through the processes of recursion and embedding that we are able to take a finite number of forms (words and phrases) and construct an infinite number of expressions. Furthermore, embedding also allows us to construct an infinitely long structure, in theory anyway.

The nursery rhyme reads as follows:

This is the house that Jack built.

This is the malt
that lay in the house that Jack built.

This is the rat
that ate the malt
that lay in the house that Jack built.

This is the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the rooster that crowed in the morn
that woke the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the farmer sowing his corn
that kept the rooster that crowed in the morn
that woke the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the horse and the hound and the horn
that belonged to the farmer sowing his corn
that kept the rooster that crowed in the morn
that woke the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

Running the tests

To run the tests, run the command busted from within the exercise directory.

Further information

For more detailed information about the Lua track, including how to get help if you're having trouble, please visit the exercism.io Lua language page.

Source

British nursery rhyme http://en.wikipedia.org/wiki/This_Is_The_House_That_Jack_Built

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

house_spec.lua

local house = require('house')

local verses = {
  [[This is the house that Jack built.]],
  [[This is the malt
that lay in the house that Jack built.]],
  [[This is the rat
that ate the malt
that lay in the house that Jack built.]],
  [[This is the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.]],
  [[This is the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.]],
  [[This is the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.]],
  [[This is the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.]],
  [[This is the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.]],
  [[This is the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.]],
  [[This is the rooster that crowed in the morn
that woke the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.]],
  [[This is the farmer sowing his corn
that kept the rooster that crowed in the morn
that woke the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.]],
  [[This is the horse and the hound and the horn
that belonged to the farmer sowing his corn
that kept the rooster that crowed in the morn
that woke the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.]]
}

describe('house', function()
  it('should correctly generate the first verse', function()
    assert.equal(verses[1], house.verse(1))
  end)

  it('should correctly generate all verses', function()
    for i = 1, #verses do
      assert.equal(verses[i], house.verse(i))
    end
  end)

  it('should recite the entire song', function()
    assert.equal(table.concat(verses, '\n'), house.recite())
  end)
end)
local lyrics_table = {
  { object = 'house',                                       description = 'Jack built.' },
  { object = 'malt',                             LF = '\n', description = 'lay in' },
  { object = 'rat',                              LF = '\n', description = 'ate' },
  { object = 'cat',                              LF = '\n', description = 'killed' },
  { object = 'dog',                              LF = '\n', description = 'worried' },
  { object = 'cow with the crumpled horn',       LF = '\n', description = 'tossed' },
  { object = 'maiden all forlorn',               LF = '\n', description = 'milked' },
  { object = 'man all tattered and torn',        LF = '\n', description = 'kissed' },
  { object = 'priest all shaven and shorn',      LF = '\n', description = 'married' },
  { object = 'rooster that crowed in the morn',  LF = '\n', description = 'woke' },
  { object = 'farmer sowing his corn',           LF = '\n', description = 'kept' },
  { object = 'horse and the hound and the horn', LF = '\n', description = 'belonged to' }
}

local function verse(index)
  local cumulated_verse = 'This is'

  for i = index, 1, -1 do
    cumulated_verse = cumulated_verse .. ' the '..lyrics_table[i].object..(lyrics_table[i].LF or ' ').. 'that '.. lyrics_table[i].description
  end
  return cumulated_verse
end

local function recite()
  local whole_song = {}
  for i = 1, #lyrics_table do
    table.insert(whole_song, verse(i))
  end
  return table.concat(whole_song, '\n')
end

return {
  verse = verse,
  recite = recite
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?