lua-users home
lua-l archive

Re: Self-awareness of functions?

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


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

AltStyle によって変換されたページ (->オリジナル) /