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#

(Cleaner still)

Date:12/07/06
Author:Don Syme
URL:n/a
Comments:0
Info:http://research.microsoft.com/fsharp
Score: (2.96 in 25 votes)
#light

open System
open System.Text
open System.Drawing
open System.Windows.Forms

let theBottleSong =
   let strSong = new StringBuilder()
   let append (s:string) = strSong.Append(s) |> ignore
   for i = 99 downto 0 do
      if (i = 0) then
         append("\nNo more bottles of beer on the wall, no more bottles of beer." +
                "\nGo to the store and buy some more, 99 bottles of beer on the wall.")
      else 
         let x = i - 1
         let plural = if (i = 1) then "" else "s"

         append (sprintf "\n%d bottle%s of beer on the wall, %d bottle%s" i plural i plural);
         append "\nTake one down and pass it around, ";

         match x with 
         | 1 -> append "1 bottle "
         | 0 -> append " no more bottles "
         | _ -> append (sprintf "%d bottles of beer on the wall.\n" x)

   strSong.ToString()
    
let form = new Form(Width=500, Height=420, 
                    Visible=true, TopMost=true, 
                    FormBorderStyle=FormBorderStyle.FixedSingle, 
                    Text="99 bottles of beer presented in F#")

let textBox = new RichTextBox(Size=new Size(480, 350), 
                              Location=new Point(5, 5),
                              Text=theBottleSong)
form.Controls.Add(textBox)

let btnClose = new Button(Location=new Point(408, 360), Text="Close")
btnClose.Click.Add (fun _ -> form.Close())
form.Controls.Add(btnClose)

[<STAThread>]
do Application.Run(form)

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
Using list comprehensionJörn Rönnow02/01/093
Recursive implementationAlec Zorab07/04/090
Tail Recursion CallSimon Kang06/22/090
lists and lambda'sKyle Eppley05/04/100

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: