Created
May 19, 2009 09:47
-
-
Save stuartloxton/114015 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-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