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#

Date:03/10/06
Author:Christoph Reiche
URL:n/a
Comments:0
Info:http://research.microsoft.com/fsharp/fsharp.aspx
Score: (3.00 in 2 votes)
(* ninetyninebottlesofbeer.fs *)

open System
open System.IO
open System.Windows.Forms

let f = new Form()
do
f.Width <- 500;
f.Height <- 420;
f.Visible <- true;
f.TopMost <- true;
f.FormBorderStyle <- System.Windows.Forms.FormBorderStyle.FixedSingle;
f.Text <- "99 bottles of beer presented in F#"

let rb = new RichTextBox()
do
f.Controls.Add(rb);
rb.Size <- new System.Drawing.Size(480, 350);
rb.Location <- new System.Drawing.Point(5, 5)

let btnClose = new Button()
do
btnClose.Location <- new System.Drawing.Point(408, 360);
btnClose.Text <- "Close";
btnClose.add_Click (new EventHandler(fun _ _ -> f.Close()));
f.Controls.Add(btnClose)

let x = ref 0
let strPlural = ref "s"
let strSong = ref ""

let CreateSong() = (
	for i = 99 downto 0 do
		if (i = 0) then (
			strSong := !strSong ^ "\r\nNo more bottles of beer on the wall, no more bottles of beer." ^
				"\r\nGo to the store and buy some more, 99 bottles of beer on the wall.";
		)
		else (
			x := i - 1;
			if (i = 1) then (
				strPlural := ""
			);

			strSong := !strSong ^ "\r\n" ^ string_of_int i ^ " bottle" ^ !strPlural ^
				" of beer on the wall, " ^ string_of_int i ^ " bottle" ^ !strPlural ^
				"  of beer." ^"\r\nTake one down and pass it around, ";

			if (!x = 1) then (
				strSong := !strSong ^ string_of_int !x ^ " bottle "
			)
			else if (!x = 0) then (
				strSong := !strSong ^ " no more bottles "
			)
			else (
				strSong := !strSong ^ string_of_int !x ^ " bottles "
			);

			strSong := !strSong ^ " of beer on the wall.\r\n";
		)
	done;
) do CreateSong();

rb.Text <- !strSong

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

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
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: