Also it could be slightly more functional :-)

By on 11/21/2006 3:05 PM ()

Yes, that's an awesome bit of full-on imperative programming. I wonder which awful language it was translated from? Two globals holding what should be local immutable values!

Here's a quick rewrite (of the core bit) - probably functional enough for this example. I submitted it to the website

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 

#light

let strSong = new StringBuilder()
let append (s:string) = strSong.Append(s) |> ignore

let CreateSong() =
    for i = 99 downto 0 do
        if (i = 0) then
            append("\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 
            let x = i - 1
            let strPlural = if (i = 1) then "" else "s"

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

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

CreateSong()

rb.Text <- strSong.ToString()

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

By on 11/21/2006 4:07 PM ()

I know this isn't an exercise in optimisation but I particularly like:

1
2
3
match x with
| 1 -> append (sprintf "%d bottle" x)
...

when you could have done:

1
| 1 -> append "1 bottle"

[;)]

Cheers,
Jon.

By on 11/22/2006 12:58 PM ()

Yes indeed!!!

By on 11/22/2006 1:02 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper