lua-users home
lua-l archive

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


On Tue, Aug 5, 2014 at 9:19 AM, Sean Conner <sean@conman.org> wrote:
> It was thus said that the Great Roberto Ierusalimschy once stated:
>> >   I don't know.  This
>> >
>> >     x = 5
>> >     function F(...)
>> >       return F(x,...)
>> >     end
>> >
>> >     print(F())
>> >
>> > is taking an awful long time to fail.
>
>   By "long time to fail" I mean "it's stuck in an infinite loop."
>
>> I believe this is O(n^2), as each new call copies all accumulated
>> arguments. Change your function to this and you will see the
>> slowdown:
>>
>> local x = 0
>> function F(...)
>>   x = x + 1
>>   print(x)
>>   return F(x,...)
>> end
>>
>> print(F())
>
>   I did that, and it's currently stuck (20 minutes) in an infinite loop.
>
>   -spc (Tail calls for the win!)

It shouldn't be infinite. When the function call accumulates enough
parameters to overflow the stack, it should fail.

/s/ Adam