exercism/elixir/proverb/lib/proverb.ex

18 lines
713 B
Elixir
Raw Normal View History

2024-08-21 15:11:30 +00:00
defmodule Proverb do
@doc """
Generate a proverb from a list of strings.
"""
@spec recite(strings :: [String.t()]) :: String.t()
def recite([]), do: ""
def recite([one]), do: first_in_the_end(one)
def recite([head | tail]), do: Enum.join([first(head), recite_proverb(tail), first_in_the_end(head)])
defp recite_proverb([last]), do: last(last)
defp recite_proverb([middle | tail]), do: middle(middle) <> " " <> recite_proverb(tail)
defp first(first), do: "For want of a #{first} "
defp middle(middle), do: "the #{middle} was lost.\nFor want of a #{middle}"
defp last(last), do: "the #{last} was lost.\n"
defp first_in_the_end(first), do: "And all for the want of a #{first}.\n"
end