(* -*- mode: sml -*-
 * $Id: ary3.smlnj,v 1.2 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 index i = i;

fun ary n =
    let
    val x = Array.array(n, 0)
    val y = Array.array(n, 0)
    fun xinit i =
        if i = n then ()
        else (Array.update(x, i, i + 1) ; xinit (i + 1))
    fun xtoy i =
        if i < 0 then ()
        else (Array.update(y, i, Array.sub(x, i) + Array.sub(y, i)) ; xtoy (i - 1))
        fun aryloop i =
        if i < 0 then ()
        else (xtoy(n-1); aryloop (i-1))
    in
    xinit 0;
    aryloop 999;
    print (Int.toString (Array.sub(y, 0)));
    print " ";
    print (Int.toString (Array.sub(y, (n-1))));
    print "\n"
    end;

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
    in
    ary num;
    OS.Process.success
    end
end

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