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 F#

(lists and lambda's)

Date:05/04/10
Author:Kyle Eppley
URL:n/a
Comments:0
Info:http://www.microsoft.com
Score: (2.57 in 7 votes)
module BeerList                     /// required for compile error in fsi

let a = " of beer "                 /// strings used 
let b = "on the wall "
let c = "take one down pass it around "
let d = "Go to the store and buy some more, 99 bottles of beer on the wall.\n"

    //build a list of bottle terms none to 99
let bottles = List.map (fun i -> i.ToString() + " bottles") [99..(-1)..2] @ 
                                             ["1 bottle";"no more bottles"]
let line1 = List.map (fun i -> i + a + b + i + a) bottles                
    //build line 1 from bottls and strings
let line2 = List.map (fun i -> c + i + a + b) bottles.Tail @ [d]    
    //build line 2 similarly
List.iter2 (fun i j -> printfn "%s\n%s\n" i j ) line1 line2         
    //output the lines interleafed

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
Using list comprehensionJörn Rönnow02/01/093
Cleaner stillDon Syme12/07/060
Recursive implementationAlec Zorab07/04/090
Tail Recursion CallSimon Kang06/22/090

Comments

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: