[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Self-awareness of functions?
- From: Wim Couwenberg <wim.couwenberg@...>
- Date: Wed, 29 Dec 2010 09:46:33 +0100
> do local fact
> fact = function(x) if x<2 then return 1 else return x*fact(x-1) end end
> fac = fact
> end
More Lua like is:
do
local function fact(x) ... end
fac = fact
end
I think there was also some discussion a long time ago to do it
something like this:
-- generic helper function to create anonymous recursive functions
function Y(f)
local function me(...) return f(me, ...) end
return me
end
-- the prototypical example..
fac = Y(function(me, n)
if n == 0 then return 1
else return n*me(n - 1)
end
end)
Bye,
Wim