(* comparing performance of factorial implementations *) (* the first factorial function *) fun fact1(n) = if n = 0 then 1 else n * fact1(n-1); (* the second factorial function *) fun afact(n, accum) = if n = 1 then accum else afact(n-1, n*accum); fun fact2(n) = afact(n, 1); (* note that loopFactorial prints the results of each iteration *) (* note also that F is a functional parameter *) fun loopFactorial(i:int,n:int,F) = if i = n then print (Int.toString( F(i) )^"\n") else let val dummy = let val ans1 = print(Int.toString( F(i) )) in print "\n" end val dummy2 = loopFactorial(i+1,n,F) in dummy2 (* any expression could be used *) end; val t1 = Timer.startCPUTimer(); loopFactorial(1,12,fact1); Timer.checkCPUTimer t1; val t2 = Timer.startCPUTimer(); loopFactorial(1,12,fact2); Timer.checkCPUTimer t2;