Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
My version of the beer koan
defmodule Beer do
@moduledoc """
# Beer Song
A program which produces the lyrics to that beloved classic, that field-trip favorite: 99 Bottles of Beer on the Wall.
Note that Not all verses are identical.
The verse for 1 bottle is as follows:
```plain
1 bottle of beer on the wall, 1 bottle of beer.
Take it down and pass it around, no more bottles of beer on the wall.
```
```plain
The verse for 0 bottles is as follows:
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.
```
You can get multiple verses by passing in the verse to start from, and (optionally) the last verse to include.
"""
def verse(0) do
"No more bottles of beer on the wall, no more bottles of beer.\n" <>
"Go to the store and buy some more, 99 bottles of beer on the wall.\n"
end
def verse(n) when is_integer(n) do
"#{bottles(n)} of beer on the wall, #{bottles(n)} of beer.\n" <>
"Take #{one_bottle(n)} down and pass it around, #{bottles(n-1)} of beer on the wall.\n"
end
def verse(n..m) do
sing n,m
end
def sing(n,m//0) do
Enum.map_join n..m, "",fn x->
verse(x) <> "\n"
end
end
defp bottles(0), do: "no more bottles"
defp bottles(1), do: "1 bottle"
defp bottles(n), do: "#{n} bottles"
defp one_bottle(1), do: "it"
defp one_bottle(_), do: "one"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment