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 Haskell

(Using guards)

Date:10/25/07
Author:Simon Johansson
URL:n/a
Comments:3
Info:n/a
Score: (3.02 in 175 votes)
--Version of http://99-bottles-of-beer.net/language-haskell-1070.html
--using guards.

bottles :: Int -> String
bottles n
        |n == 0 = "no more bottles"
        |n == 1 = "1 bottle"
        |n >  1 = show n ++ " bottles"

verse :: Int -> String
verse n
        |n == 0 = "No more bottles of beer on the wall, no more bottles of beer.\n"
                   ++ "Go to the store and buy some more, 99 bottles of beer on the wall."
        |n>0  = bottles n ++ " of beer on the wall, " ++ bottles n ++ " of beer.\n"
                   ++ "Take one down and pass it around, " ++ bottles (n-1)
                   ++ " of beer on the wall.\n"

main      = mapM (putStrLn . verse) [99,98..0]

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
1Iavor03/03/065
With monads and monad transformerAdrien Piérard12/25/062
Using list comprehensionBen Firner11/21/080

Comments

>>  George said on 05/27/08 15:37:26

George This is actually really nice.
When the time is right im dropping imperative for functional programming.

>>  Neil Kandalgaonkar said on 06/02/09 00:10:07

Neil Kandalgaonkar The useless definition of bottles 0 bothers me.

>>   said on 06/12/09 19:49:15

Neil, the definition of bottles 0 is useful, in the verse for 1 bottle.

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: