Anonymous recursion: Difference between revisions

no edit summary
No edit summary
Line 256:
 
Note that <code>arguments.callee</code> will not be available in ES5 Strict mode.
 
=={{header|Lua}}==
Using a [[Y combinator]].
 
<lang lua>
 
local function Y(x) return (function (f) return f(f) end)(function(y) return x(function(z) return y(y)(z) end) end) end
 
return Y(function(fibs)
return function(n)
return n < 2 and 1 or fibs(n - 1) + fibs(n - 2)
end
end)
 
</lang>
 
using a metatable (also achieves memoization)
 
<lang lua>
 
return setmetatable({1,1},{__index = function(self, n)
self[n] = self[n-1] + self[n-2]
return self[n]
end})
 
</lang>
 
=={{header|Mathematica}}==
Anonymous user