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

Tuesday, October 28, 2014

[mLite] Learning an ML dialect

I've tried to get my head into functional languages for some time. This time I'm having a go at mLite, which the author, Nils M Holm, describes as "a lightweight (and slightly odd) inhabitant of the ML universe ... Much like ML, but with dynamic typingguards, and a Haskell-style apply operator."

I've created a presence on RosettaCode for mLite and solved the Ackermann function challenge.

© Bruce M. Axtens, 2014.