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

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

fun ack(0,n) = n+1
  | ack(m,0) = ack(m-1,1)
  | ack(m,n) = ack(m-1,ack(m,(n-1)));

fun atoi s = case Int.fromString s of SOME num => num | NONE => 0;
fun printl [] = print "\n" | printl(h::t) = ( print h ; printl t );

fun main(name, args) = 
  let
    val arg = hd(args @ ["1"]);
    val num = atoi arg;
    val ack = ack(3, num);
    val result = Int.toString ack;
  in
      printl ["Ack(3,", arg, "): ", result];
      OS.Process.success
  end
end

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