lua-users home
lua-l archive

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


On 8/4/2014 8:58 AM, Jay Carlson wrote:
On Aug 4, 2014, at 9:40 AM, Andrew Starks <andrew.starks@trms.com> wrote:

On Sun, Aug 3, 2014 at 2:03 PM, Tom N Harris <telliamed@whoopdedo.org> wrote:
I'll have to remember this the next time I feel the urge to write `return
value, recursive_call(...)` which isn't a tail call. Although I've
occasionally thought the VM could pull some stack manipulation tricks to make
it a tail call.
Hmm... I had to look this up. I thought that `return value, recursive_call(...)` would be a tail call. I guess it isn't, given that `return fund(args)` is the required form.
This gets into what "tail call" means, and perhaps specifically what "properly tail recursive" means. As a matter of language specification, the call to f in

   function f()
     return 1, f()
   end

is difficult to require to be a tail call, since the return values stack will exhaust all memory regardless of whether activation records do.

You can watch Scheme fight it out here:

https://groups.csail.mit.edu/mac/ftpdir/scheme-mail/HTML/rrrs-1997/msg00048.html

Jay

I always understood the documentation that "tail call" to mean

 function f(args)
    --optional other code
    return g(otherargs)
end

and "recursive tail call" to mean

function f(args)
    --other code including code to end recursion
    return f(modified_args)
end

and in both cases the return statement must return exactly the unmodified return values of the function it calls; otherwise it is not a tail call. IMHO, the Lua documentation is clear on this point.