Skip to content

Anonymous recursion

editable example

Click the pencil to open this in an editor, change it, and run it in your browser. The same solution is posted on Rosetta Code.

ghul
use IO.Std.write_line
let fibonacci = n =>
assert n >= 0 else "fibonacci is not defined for a negative argument" in
(i rec => if i < 2 then i else rec(i - 1) + rec(i - 2) fi)(n)
for n in 0::10 do
write_line("fibonacci({n}) = {fibonacci(n)}")
od