Update for doxygen 1.5.5, graph generation, and match current code.
[tagua/yd.git] / lua / test / factorial.lua
blob7c4cf0fa45da1780292b8bc5e6e99cc16e4c6711
1 -- function closures are powerful
3 -- traditional fixed-point operator from functional programming
4 Y = function (g)
5 local a = function (f) return f(f) end
6 return a(function (f)
7 return g(function (x)
8 local c=f(f)
9 return c(x)
10 end)
11 end)
12 end
15 -- factorial without recursion
16 F = function (f)
17 return function (n)
18 if n == 0 then return 1
19 else return n*f(n-1) end
20 end
21 end
23 factorial = Y(F) -- factorial is the fixed point of F
25 -- now test it
26 function test(x)
27 io.write(x,"! = ",factorial(x),"\n")
28 end
30 for n=0,16 do
31 test(n)
32 end