Path: blob/devel/contrib/lua-5.1.5/test/factorial.lua
3203 views
-- function closures are powerful12-- traditional fixed-point operator from functional programming3Y = function (g)4local a = function (f) return f(f) end5return a(function (f)6return g(function (x)7local c=f(f)8return c(x)9end)10end)11end121314-- factorial without recursion15F = function (f)16return function (n)17if n == 0 then return 118else return n*f(n-1) end19end20end2122factorial = Y(F) -- factorial is the fixed point of F2324-- now test it25function test(x)26io.write(x,"! = ",factorial(x),"\n")27end2829for n=0,16 do30test(n)31end323334