exercism/elixir/house/lib/house.ex

37 lines
926 B
Elixir
Raw Normal View History

2024-07-04 21:39:14 +00:00
defmodule House do
@doc """
Return verses of the nursery rhyme 'This is the House that Jack Built'.
"""
@this_is "This is"
@verses [
"house that Jack built",
"malt that lay in",
"rat that ate",
"cat that killed",
"dog that worried",
"cow with the crumpled horn that tossed",
"maiden all forlorn that milked",
"man all tattered and torn that kissed",
"priest all shaven and shorn that married",
"rooster that crowed in the morn that woke",
"farmer sowing his corn that kept",
"horse and the hound and the horn that belonged to"
]
@spec recite(start :: integer, stop :: integer) :: String.t()
def recite(start, stop) do
(Enum.map(start..stop, &verse/1) |> Enum.join("\n")) <> "\n"
end
defp verse(i) do
verse =
@verses
|> Enum.take(i)
|> Enum.reverse()
|> Enum.join(" the ")
@this_is <> " the " <> verse <> "."
end
end