2007-09-04 01:52:33 +00:00
|
|
|
local function f(x)
|
|
|
|
|
-- tailcall to a builtin
|
|
|
|
|
return math.sin(x)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function factorial(i)
|
2007-09-05 03:02:57 +00:00
|
|
|
local function helper(product, n)
|
2007-09-04 01:52:33 +00:00
|
|
|
if n <= 0 then
|
2007-09-05 03:02:57 +00:00
|
|
|
return product
|
2007-09-04 01:52:33 +00:00
|
|
|
else
|
|
|
|
|
-- tail call to a nested Lua function
|
2007-09-05 03:02:57 +00:00
|
|
|
return helper(n * product, n - 1)
|
2007-09-04 01:52:33 +00:00
|
|
|
end
|
|
|
|
|
end
|
2007-09-05 03:02:57 +00:00
|
|
|
return helper(1, i)
|
2007-09-04 01:52:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local result1 = factorial(5)
|
|
|
|
|
print(result1)
|
|
|
|
|
print(factorial(5))
|
|
|
|
|
|
|
|
|
|
local result2 = f(math.pi)
|
|
|
|
|
print(result2)
|
|
|
|
|
print(f(math.pi))
|