-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).