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

(functional)

Date:09/29/05
Author:Peter Kirby
URL:http://www.peterkirby.com/
Comments:2
Info:http://www.haskell.org/
Score: (4.00 in 29 votes)
module Main where
import Char(toUpper)

beers n | n == 0    = "no more bottles of beer"
        | n == 1    = "1 more bottle of beer"
        | otherwise = show n ++ " bottles of beer"

onwall n = beers n ++ " on the wall, " ++ beers n ++ ".\n"

around n | n == 0    = "go to the store and buy some more, " ++ beers 99 ++ " on the wall.\n"
         | otherwise = "take one down and pass it around, " ++ beers (n-1) ++ " on the wall.\n"

capitalize s = [toUpper(head s)] ++ (tail s)

verse n = capitalize (onwall n) ++ capitalize (around n) ++ "\n"

main = putStr $ concatMap verse [99, 98 .. 0]

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
Using guardsSimon Johansson10/25/071
2Iavor03/03/063
With monads and monad transformerAdrien Piérard12/25/062

Comments

>>  lomeo said on 03/30/06 10:12:05

lomeo Peter dont use pattern matching that could help to simplify the code:

beers 0 = "no more bottles of beer"
beers 1 = "1 more bottle of beer"
beers n = show n ++ " bottles of beer"

capitalize (x:xs) = toUpper x : xs

etc.

>>  Andriy said on 09/24/06 15:21:22

Andriy Thanks to Peter Kirby for the program and to lomeo for valuable comment.

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: