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

(advanced with exact last verse)

Date:06/27/05
Author:Kurt J. Bosch
URL:n/a
Comments:1
Info:http://www.erlang.se/
Score: (2.78 in 110 votes)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% erlang - 99 Bottles of Beer Song
%%
%% Advanced Edition (makes an exact last verse too ;-)
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% Kurt J. Bosch
%%
%% 2005/06/27
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

-module(beers).
-export([sing/0]).

sing ()  -> sing(99).

sing (N) -> verse( N, N ).

% ---------------------------------------------------------------------------

% the last verse
verse( 0, Nstart ) ->
  line ( [
              num_bottles_of_beer( 0     , u_case, bottle  ),
              " on the wall. "                              ,
              num_bottles_of_beer( 0     , u_case, bottles ),
              "..."                                            ] ),
  line ( [
              "Go to the store and buy some more...",
              num_bottles_of_beer( Nstart, l_case ) ,
              "."                                              ] );

% the other verses
verse( N, Nstart ) ->
  line ( [
              num_bottles_of_beer( N     , u_case ),
              " on the wall, "                     ,
              num_bottles_of_beer( N     , l_case ),
              "."                                              ] ),
  line ( [
              "Take one down and pass it around, " ,
              num_bottles_of_beer( N-1   , l_case ),
              " on the wall."                                  ] ),

  line ( [] ),

  verse (N-1, Nstart).

% ---------------------------------------------------------------------------

line (List) ->
  io:put_chars (List),
  io:nl().

% ---------------------------------------------------------------------------

num_bottles_of_beer (1, Case) -> num_bottles_of_beer (1, Case, bottle );
num_bottles_of_beer (N, Case) -> num_bottles_of_beer (N, Case, bottles).

num_bottles_of_beer (N, Case, Bottle_s) ->
  [ num_prefix(N), bottle(N, Case), s_postfix(Bottle_s), " of beer" ].

num_prefix (0) -> "";
num_prefix (N) -> lists:concat ( [ (N), " " ] ). % integer to string

s_postfix (bottles) -> "s";
s_postfix (bottle ) -> "".

% ---------------------------------------------------------------------------

bottle       (0, Case) -> N = letter (n, Case), [ N, "o more bottle" ];
bottle       (_, Case) -> B = letter (b, Case), [ B, "ottle"         ].

letter (n, l_case) -> "n";
letter (n, u_case) -> "N";
letter (b, l_case) -> "b";
letter (b, u_case) -> "B".

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
Concurrent solutionBill Clementson05/15/076
proper version - simple codeHåkan Stenholm01/23/061

Comments

>>  Mia34uC said on 01/02/10 07:10:21

Mia34uC

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: