val i = ref 0; while !i < 10 do (print(Int.toString(!i) ^ "\n"); i := !i + 1); fun multiply(x, 0) = 0 | multiply(x, y) = x + multiply(x, y-1); fun multiply(x, y) = let val prod = ref 0 val i = ref 0 in (while !i < y do (prod := !prod + x; i := !i + 1); !prod) end; fun exp(x, 0) = 1 | exp(x, y) = x * exp(x, y-1); fun exp(x, y) = let val expon = ref 1; val i = ref 0; in (while !i < y do (expon := !expon * x; i := !i + 1); !expon) end; fun gcd(a, 0) = a | gcd(a, b) = gcd(b, a mod b); fun gcd(a0, b0) = let val a = ref a0; val b = ref b0; in (while !b > 0 do let val r = !a mod !b in (a := !b; b := r) end; !a) end; fun count([]) = 0 | count(a::rest) = 1 + count(rest); fun count(LL) = let val restLL = ref LL; val num = ref 0; in (while !restLL <> [] do (num := !num + 1; restLL := tl(!restLL)); !num) end; fun arithSum(1) = 1 | arithSum(N) = N + arithSum(N-1); fun arithSum(N) = let val s = ref 0; val i = ref 1; in (while !i <= N do (s := !s + !i ; i := !i + 1); !s) end; fun remainder(a, b) = let val q = a div b; val p = q * b; val r = a - p; in r end; (* From Ex 6.10.1 *) fun aaa(m) = let val x = ref 0; val i = ref 0; in (while !i < m do (x := !x + 2 * !i; i := !i + 1); !x) end; fun exp(x, y) = let fun helper(a, 0) = a | helper(a, i) = helper(a * x, i-1); in helper(1, y) end; fun exp(x, y) = let val a = ref 1; val i = ref y; in (while !i > 0 do (i := !i - 1; a := !a * x); !a) end;