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

Date:03/03/06
Author:Iavor
URL:n/a
Comments:5
Info:http://www.haskell.org
Score: (4.02 in 487 votes)
bottles 0 = "no more bottles"
bottles 1 = "1 bottle"
bottles n = show n ++ " bottles"

verse 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."

verse n   = 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
Using guardsSimon Johansson10/25/073
With monads and monad transformerAdrien Piérard12/25/062
Using list comprehensionBen Firner11/21/080

Comments

>>  xenon said on 11/27/06 12:08:09

xenon Haha this one is great.
I've coded some OPAL (http://uebb.cs.tu-berlin.de/~opal/) and it's similar to haskell.

*still laughing*

:-)

>>  Simon Johansson said on 10/25/07 16:22:18

Simon Johansson Why not use guards?
It makes the code much more readable(not that your code isn't clear :) )

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]

>>  Incisura said on 05/20/08 20:02:51

Incisura In what way does the guards make the workings of the code clearer?
From my view they simply add dead meat and greater resemblance to a case dispatcher.
You have applied some unity of form but I will not have it at the price of having to say n == 0 = "..."
when pattern matching is the syntactical sugar up to this simple task.

>>  Helge said on 03/12/10 13:41:20

Helge I agree with Incisura - the guards just make the code ugly.

>>  Anon Bast said on 08/06/10 08:52:54

Anon Bast I'm not familiar with Haskell, but I could understand the code very clearly. The alternate versions didn't make it any easier to read.

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: