[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Recursive anonymous functions?
- From: Wim Couwenberg <debosberg@...>
- Date: Thu, 22 Jul 2004 14:14:02 -0700 (PDT)
Maybe a more Lua-friendly way to write Gabor's
proposal would be the function `reflect':
function reflect(f)
local function me(...)
return f(me, unpack(arg))
end
return me
end
With this helper you can easily express recursive
functions, but you must introduce an additional first
argument:
fac = reflect(function(me, n)
if n == 0 then
return 1
else
return n*me(n - 1)
end
end)
for i = 0, 10 do
print(i, fac(i))
end
Of course, in this example the first thing I do is to
give the anonymous function a name, but anyway...
--
Wim
__________________________________
Do you Yahoo!?
Vote for the stars of Yahoo!'s next ad campaign!
http://advision.webevents.yahoo.com/yahoo/votelifeengine/