Factorial in OCaml
Example for versions
OCaml 3.11
This example uses an auxiliary function fact
, so that tail recursion is possible.
let rec fact n accum =
if n <= 1 then
accum
else
fact (n-1) (accum*n);;
let factorial n =
fact n 1;;
let () =
for n = 0 to 16 do
Printf.printf "%d! = %d\n" n (factorial n)
done;
Comments
]]>blog comments powered by Disqus
]]>