Language Erlang
(proper version - simple code)
Date: | 01/23/06 |
Author: | Håkan Stenholm |
URL: | n/a |
Comments: | 1 |
Info: | http://www.erlang.org |
Score: | (2.81 in 26 votes) |
-module(bottle). %% only the 0 argument version of song(...) can be called by other modules -export([song/0]). %% macros to reduce function name length -define(dup, lists:duplicate). -define(i2l, integer_to_list). %% initiate the bottle count song() -> song(99). %% base case of the recursion (bottles = 0), print the special second line message song(0) -> first_line(0), io:format("Go to the store and buy some more, 99 bottles of beer on the wall.\n"); %% no. of bottles still > 0, print the usual second line song(N) -> first_line(N), %% only use the lower case bottle(...) return value io:format("Take one down and pass it around, ~s of beer on the wall.~n~n", tl(bottle(N-1))), song(N-1). %% prints the top line first_line(N) -> io:format("~s of beer on the wall, ~s of beer.~n", bottle(N)). %% return both upper and lower case versions in a list, this makes them directly suitable %% as arguments to the print function - io:format(...) bottle(N) -> case N of 0 -> ["No more bottles", "no more bottles"]; 1 -> ?dup(2, "1 bottle"); N -> ?dup(2, ?i2l(N) ++ " bottles") end.
Download Source | Write Comment
Alternative Versions
Version | Author | Date | Comments | Rate |
---|---|---|---|---|
Concurrent solution | Bill Clementson | 05/15/07 | 6 | |
advanced with exact last verse | Kurt J. Bosch | 06/27/05 | 1 |
Download Source | Write Comment
Add Comment
Please provide a value for the fields Name,
Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.
Please don't post large portions of code here! Use the form to submit new examples or updates instead!
Comments
niahoo said on 09/22/10 14:28:39
Yeah, this one is cool ; simple and fast.