nestedloop.mlton

Stephen Weeks MLton@sourcelight.com
Tue, 10 Jul 2001 16:09:56 -0700


Here is a more idiomatic version of nestedloop.mlton.


fun atoi s = case Int.fromString s of SOME num => num | NONE => 0

fun main (name, args) = 
  let
    val arg = hd (args @ ["1"])
    val n = atoi arg
    fun f (ac, g) =
       let
	  fun loop (i, ac) =
	     if i = 0
		then ac
	     else loop (i - 1, g ac)
       in
	  loop (n, ac)
       end
    val result =
       f (0, fn x =>
	  f (x, fn x =>
	     f (x, fn x =>
		f (x, fn x =>
		   f (x, fn x =>
		      f (x, fn x => x + 1))))))
  in (
      print (Int.toString result); print "\n";
      OS.Process.success
  ) end;

val _ = main ( CommandLine.name (), CommandLine.arguments () );