(* -*- mode: sml -*-
 * $Id: nestedloop.smlnj,v 1.3 2001/07/09 00:25:28 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 *)

structure Test : sig
    val main : (string * string list) -> OS.Process.status
end = struct

fun loopF 0 x = x
  | loopF n x = loopF (n-1) (x+1)

fun loopE 0 m x = x
  | loopE n m x = loopE (n-1) m (loopF m x);

fun loopD 0 m x = x
  | loopD n m x = loopD (n-1) m (loopE m m x);

fun loopC 0 m x = x
  | loopC n m x = loopC (n-1) m (loopD m m x);

fun loopB 0 m x = x
  | loopB n m x = loopB (n-1) m (loopC m m x);

fun loopA 0 m x = x
  | loopA n m x = loopA (n-1) m (loopB m m x);


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

fun main(name, args) = 
  let
    val arg = hd(args @ ["1"]);
    val num = atoi arg;
    val result = loopA num num 0
  in
      print (Int.toString result); print "\n";
      OS.Process.success
  end
end

val _ = SMLofNJ.exportFn("nestedloop", Test.main);