Skip to content

Instantly share code, notes, and snippets.

@aaronjensen
Last active July 21, 2016 20:18
Show Gist options
  • Select an option

  • Save aaronjensen/d273ddbee9f5fdafab28111268071a54 to your computer and use it in GitHub Desktop.

Select an option

Save aaronjensen/d273ddbee9f5fdafab28111268071a54 to your computer and use it in GitHub Desktop.
defmodule Bottles do
def song, do: verses(99, 0)
def verses(first, last) do
Enum.map_join first..last, "\n", &verse/1
end
def verse(0) do
"""
No more bottles of beer on the wall, no more bottles of beer.
Go to the store and buy some more, 99 bottles of beer on the wall.
"""
end
def verse(n) do
"""
#{bottles(n)} of beer on the wall, #{bottles(n)} of beer.
Take one down and pass it around, #{bottles(n - 1)} of beer on the wall.
"""
end
defp bottles(0), do: "No more bottles"
defp bottles(1), do: "1 bottle"
defp bottles(n), do: "#{n} bottles"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment