Skip to content

Instantly share code, notes, and snippets.

@ChrisWhealy
Created August 10, 2017 11:58
Show Gist options
  • Save ChrisWhealy/c4125ec010199f57b7517544471d4733 to your computer and use it in GitHub Desktop.
Save ChrisWhealy/c4125ec010199f57b7517544471d4733 to your computer and use it in GitHub Desktop.
-module(beersong).
-export([sing/0]).
-define(LIMIT, 99).
-define(TEMPLATE_0, "~s of beer on the wall, ~s of beer.~nGo to the store and buy some more, 99 bottles of beer on the wall.~n").
-define(TEMPLATE_N, "~s of beer on the wall, ~s of beer.~nTake one down and pass it around, ~s of beer on the wall.~n~n").
print_verse(0) -> io:format(?TEMPLATE_0, phrase(0));
print_verse(Bottle) -> io:format(?TEMPLATE_N, phrase(Bottle)).
phrase(0) -> ["No more bottles", "no more bottles"];
phrase(1) -> ["1 bottle", "1 bottle", "no more bottles"];
phrase(2) -> ["2 bottles", "2 bottles", "1 bottle"];
phrase(Bottle) -> lists:duplicate(2, integer_to_list(Bottle) ++ " bottles") ++
[integer_to_list(Bottle-1) ++ " bottles"].
sing() -> lists:foreach(fun print_verse/1, lists:seq(?LIMIT,0,-1)).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment