Re: Self-awareness of functions?
[
Date Prev][
Date Next][
Thread Prev][
Thread Next]
[
Date Index]
[
Thread Index]
- Subject: Re: Self-awareness of functions?
- From: David Kastrup <dak@...>
- Date: 2010年12月29日 09:50:10 +0100
Dirk Laurie <dpl@sun.ac.za> writes:
> In Lua you must take care if you want to define a global recursive
> function. Thus:
>
> do local fact
> fact = function(x) if x<2 then return 1 else return x*fact(x-1) end end
> fac = fact
> end
> fact = "Napoleon escaped from Elba."
> print(fac(5)) --> 120
>
> You can't omit any of that padding: create a local variable, then
> assign it to a global variable, all in an enclosing block.
do
local function fact(x) if x<2 then return 1 else return x*fact(x-1)
end end
fac = fact
end
fact = "Napoleon escaped from Elba."
print(fac(5))
is a bit more compact on the eyes.
--
David Kastrup