lua-users home
lua-l archive

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


Or possibly simpler:

do
 local function _fact(n)
       if n > 1 then return n * _fact( n - 1 ) end
       return 1
   end
 fact = _fact
end

- Alex

On Mon, Mar 23, 2009 at 12:01 PM, David Kastrup <dak@gnu.org> wrote:

Ok, call me stupid, but a recursive function in my book would be one
that can call itself and is self-contained.

If I do something like

function fact(n)
if n>1 then
return n*fact(n-1)
end
return 1
end

then this is _not_ a self-contained recursive function, since it relies
on calling itself through the variable named fact. If I do

factx=fact
fact=nil
print(factx(6))

this does not work. We had some previous discussion about recursive
stuff here (with some fairly esoteric code partly), but what would most
likely be the _simplest_ way to get a self-contained recursive function?

--
David Kastrup