~tim/lydia

199c6f6b0a5cb92b6aa98625a485f11d3a633f51 — Tim Morgan 8 years ago 9b12b4c
Example of a tail-call optimized recursive function.
1 files changed, 7 insertions(+), 0 deletions(-)

M example.lid
M example.lid => example.lid +7 -0
@@ 21,6 21,13 @@ fact = [x] { if x == 1,
                { x * (fact x - 1) } }
out (fact 20)

# recursive functions: factorial (tail-call optimized)
fact2 = [x] { f = [x a] { if x == 1,
                             a,
                             { (f x - 1, a * x) } }
              f x 1 }
out (fact2 20)

# recursive functions: fibonacci sequence
fib = [n] { if n < 2,
               n,