|
Dirk Laurie wrote:
On Wed, Dec 29, 2010 at 09:22:36AM +0200, Dirk Laurie wrote:Because of the need for a local name for the function, you can't definean anonymous recursive function, e.g. for use in an argument list.
[snip]
1. The construction "function(x) ... end" defines an anonymous function. 2. "function foo(x)" instead of "foo=function(x)" is mere syntactic sugar. You are actually doing the same thing. 3. Anonymous functions are not only assigned. They can also appear in argument lists and arrays. So we really use them. 4. There appears to be no way to define an anonymous recursive function. 5. Therefore recursive functions in argument lists and tables must be named functions. 6. Unless one can find a way to mimic them by some sort of anonymous recursive function constructor.
[snip] Wouldn't this idiom suffice? fac = (function() local function fact(x) if x<2 then return 1 else return x*fact(x-1) end end return fact end)() fact = "Napoleon escaped from Elba." print(fac(5)) --> 120Maybe it's verbose and inefficient when creating the closure, but not horribly unreadable IMHO. And it works in arg lists and tables as it wraps the function creation as an expression.
Dirk
Best Regards -- Lorenzo