House
Recite “This is the House that Jack Built”, where each verse embeds every phrase of the verse before.
Adapted from Exercism’s problem specification (MIT). Listed under Strings & Text.
// FILE: house.nomi
//# The nursery rhyme "This is the House that Jack Built".
/// Each verse's new phrase, in the order the rhyme adds them. A phrase ends
/// with what its subject did to the subject of the phrase before it.
once phrases = [
"the house that Jack built",
"the malt that lay in",
"the rat that ate",
"the cat that killed",
"the dog that worried",
"the cow with the crumpled horn that tossed",
"the maiden all forlorn that milked",
"the man all tattered and torn that kissed",
"the priest all shaven and shorn that married",
"the rooster that crowed in the morn that woke",
"the farmer sowing his corn that kept",
"the horse and the hound and the horn that belonged to",
]
/// The verses `start` to `end`, both included, counting from 1.
pub fn recite(start: Int, stop: Int) -> List<String>
start..=stop
|> Iter.map(verse)
|> Iter.to_list()
end
/// Verse `number` embeds every earlier phrase, newest first.
fn verse(number: Int) -> String
embedded =
phrases
|> Iter.take(number)
|> Iter.reverse()
|> String.join(" ")
"This is ${embedded}."
end
// FILE: house_test.nomi
import house
/// Each canonical verse on its own: its description, its number and its text.
once verses = [
("verse one - the house that jack built", 1, "This is the house that Jack built."),
(
"verse two - the malt that lay",
2,
"This is the malt that lay in the house that Jack built.",
),
(
"verse three - the rat that ate",
3,
"This is the rat that ate the malt that lay in the house that Jack built.",
),
(
"verse four - the cat that killed",
4,
"This is the cat that killed the rat that ate the malt that lay in the house that Jack built.",
),
(
"verse five - the dog that worried",
5,
"This is the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.",
),
(
"verse six - the cow with the crumpled horn",
6,
"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.",
),
(
"verse seven - the maiden all forlorn",
7,
"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.",
),
(
"verse eight - the man all tattered and torn",
8,
"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.",
),
(
"verse nine - the priest all shaven and shorn",
9,
"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.",
),
(
"verse 10 - the rooster that crowed in the morn",
10,
"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.",
),
(
"verse 11 - the farmer sowing his corn",
11,
"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.",
),
(
"verse 12 - the horse and the hound and the horn",
12,
"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.",
),
]
test "single verses"
subtests verses, (description, number, expected), description
assert house.recite(number, number) == [expected]
end
end
test "several verses"
[
("multiple verses", 4, 8),
("full rhyme", 1, 12),
]
|> subtests (description, start, stop), description
expected =
verses
|> Iter.drop(start - 1)
|> Iter.take(stop - start + 1)
|> Iter.map(.third)
|> Iter.to_list()
assert house.recite(start, stop) == expected
end
endok single verses (12 cases)
ok several verses (2 cases)
test result: ok. 14 passed, 0 failed
How it’s written
Section titled “How it’s written”- The rhyme is one list of phrases, each ending with what its subject did to the next one down (“the rat that ate”). A verse is the first
nphrases, reversed and joined with spaces, so no phrase needs a special case: the house’s phrase is the only one without a verb, and it always comes last. - The alternative, a list of
(subject, verb)pairs joined with ” that ”, splits each line of the rhyme in two for no gain in any verse. recitemaps a range of verse numbers, like twelve-days.- The test file holds each canonical verse once, in a
once versestable; the two multi-verse cases take their expected verses from it, since each is exactly those verses in order.