Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

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

VersionAuthorDateCommentsRate
Concurrent solutionBill Clementson05/15/076
advanced with exact last verseKurt J. Bosch06/27/051

Comments

>>  niahoo said on 09/22/10 14:28:39

niahoo Yeah, this one is cool ; simple and fast.

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!

Name:

eMail:

URL:

Security Code:
  
Comment: