/* original version from the LUA 4.0 programming language distribution http://www.lua.org */ // function closures are powerful // traditional fixed-point operator from functional programming Y <- function (g){ local a = function (f) return f(f) return a(function (f):(g){ return g(function (x):(f) { local c=f(f) return c(x) }) }) } // factorial without recursion F <- function (f){ return function (n):(f){ if (n == 0) return 1 else return n*f(n-1) } } factorial <- Y(F) // factorial is the fixed point of F // now test it function test(x){ print(x+"! = "+factorial(x)+"\n") } test(3) test(4) test(5) test(6) test(7)