Wednesday, October 29, 2014

[mLite] 99 bottles of beer

99 bottles of beer is often the first thing I try to do in a (new for me) programming language. Here is an mLite implementation (which can also be found now on RosettaCode.)
val NL = implode [#"newline"]

fun itone 1 = "it" 
        | n = "one"

fun plural (s, 0) = ("no " @ s @ "s") 
                    | (s, 1) = ("1 " @ s) 
                    | (s, n) = (ntos n @ " " @ s @ "s")

fun verse 0 = "no bottles of beer on the wall" @ NL @ 
              "no bottles of beer" @ NL @ 
              "go to the store and buy some more" @ NL @ 
              "99 bottles of beer on the wall" @ NL @ NL 
        | x = plural ("bottle",x) @ " of beer on the wall" @ NL @ 
              plural ("bottle",x) @ " of beer" @ NL @ 
              "take " @ (itone x) @ " down and pass it round" @ NL @ 
              plural ("bottle", (x-1)) @ " of beer on the wall" @ NL @ NL

fun bottles x = map (print o verse) (rev (0 :: iota (1, x)))

fun default (false, y) = y | (x, _) = x

;
bottles (ston (default (argv 0, "99")))
This code allows one to specify how many bottles on the command line (e.g.
mlite -f 99bob.m 4
), defaulting to the usual 99.

© Copyright Bruce M. Axtens, 2014

No comments: