[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Fragile Recursion?
- From: Martin Guy <martinwguy@...>
- Date: Tue, 10 Apr 2012 14:20:26 +0200
On 10 April 2012 09:29, Miles Bader <miles@gnu.org> wrote:
> Tim Hill <drtimhill@gmail.com> writes:
>> 2. Wrap the function in a closure that contains a reference to
>> itself and recurse using the closure. Something like this:
>>
>> local function f1(n)
>> if n == 0 then return 1 end
>> return n * f1(n-1)
>> end
>> fact = f1
>>
>> This works but it takes the performance hit of the closure just to
>> get the recursion.
>
> It doesn't seem like much of a "performance hit", as a closure is only
> created once, when the function is defined.
...and you save the global table lookup of "fact" in the recursion,
which turns out to be faster.
M