Skip to content

Instantly share code, notes, and snippets.

@stuartloxton
Created May 19, 2009 09:47
Show Gist options
  • Save stuartloxton/114015 to your computer and use it in GitHub Desktop.
Save stuartloxton/114015 to your computer and use it in GitHub Desktop.
-module (beersong).
-author("Stuart Loxton").
-export ([sing/1]).
-define(TEMPLATE_0, "~s of beer on the wall, ~s of beer.~n"
"Go to the store and buy some more,"
).
-define(TEMPLATE_N, "~s of beer on the wall, ~s of beer.~n"
"Take one down and pass it around, ~s"
" of beer on the wall.~n~n").
verse(0) -> io:format(?TEMPLATE_0, ['No more bottles', 'no more bottles']);
verse(1) -> io:format(?TEMPLATE_N, ['1 bottle', '1 bottle', 'no more bottles']);
verse(2) -> io:format(?TEMPLATE_N, ['2 bottles', '2 bottles', '1 bottle']);
verse(Bottles) -> io:format(?TEMPLATE_N, lists:duplicate(2, integer_to_list(Bottles) ++ " bottles") ++ [integer_to_list(Bottles-1) ++ "bottle"]).
sing(0, O) -> verse(0), io:format("~p bottles of beer on the wall.~n", [O]);
sing(N, O) -> verse(N), sing(N - 1, O).
sing(N) -> sing(N, N).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment