lua-users home
lua-l archive

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


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