[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Self-awareness of functions?
- From: Dirk Laurie <dpl@...>
- Date: Thu, 30 Dec 2010 08:26:36 +0200
On Wed, Dec 29, 2010 at 09:30:28PM +0200, Lorenzo Donati wrote:
> 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 define
> >> an anonymous recursive function, e.g. for use in an argument list.
>
> 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)) --> 120
>
It took me a minute to figure out why it works, but many idioms are
like that.
It's got this over the "Y constructor" that 'fact' remains a function
of one argument. But is it easier to read?
Dirk