summaryrefslogtreecommitdiff
path: root/test/factorial.lua
diff options
context:
space:
mode:
Diffstat (limited to 'test/factorial.lua')
-rw-r--r--test/factorial.lua19
1 files changed, 7 insertions, 12 deletions
diff --git a/test/factorial.lua b/test/factorial.lua
index d9cc375c..7c4cf0fa 100644
--- a/test/factorial.lua
+++ b/test/factorial.lua
@@ -1,12 +1,11 @@
-- function closures are powerful
-- traditional fixed-point operator from functional programming
-
Y = function (g)
local a = function (f) return f(f) end
return a(function (f)
- return %g(function (x)
- local c=%f(%f)
+ return g(function (x)
+ local c=f(f)
return c(x)
end)
end)
@@ -14,24 +13,20 @@ end
-- factorial without recursion
-
F = function (f)
return function (n)
if n == 0 then return 1
- else return n*%f(n-1) end
+ else return n*f(n-1) end
end
end
factorial = Y(F) -- factorial is the fixed point of F
-- now test it
-
function test(x)
- write(x,"! = ",factorial(x),"\n")
+ io.write(x,"! = ",factorial(x),"\n")
end
-test(3)
-test(4)
-test(5)
-test(6)
-test(7)
+for n=0,16 do
+ test(n)
+end