[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: How to do actual recursive functions in Lua?
- From: David Given <dg@...>
- Date: Mon, 23 Mar 2009 15:12:31 +0000
-----BEGIN PGP SIGNED MESSAGE-----
David Kastrup wrote:
> Ok, call me stupid, but a recursive function in my book would be one
> that can call itself and is self-contained.
> then this is _not_ a self-contained recursive function, since it relies
> on calling itself through the variable named fact.
Well, I'm not entire sure what such a thing would be *useful* for, but
you should be able to achieve what you want using the following foul hack:
local self = debug.getinfo(1, "f").func
if (n>1) then
But this will be horribly, *horribly* slower.
If what you want is to prevent people from fiddling with the innards of
your recursive function, would not something like this suffice?
local fact = fact
fact = function(n)
...same function body here...
Now the 'fact' that's called by the function is safely enclosed in an
upvalue in an isolated scope and can't be modified. Will that do?
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
-----END PGP SIGNATURE-----