[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Confused about proper tail calls
- From: RLake@...
- Date: Mon, 29 Sep 2003 19:38:21 -0500
> I originally understood this as: when function A makes a proper tail
call
> to function B, the stack containing A's activation record is *replaced*
> by that of B (since A's stack entry is reused).
Yes, that is what happens.
> However, this does not seem to be true. When I ran a script containing
> a recursive function, this is the call stack I got:
However, Lua goes to some trouble to fake a call stack for you, as you
see:
> ========= CALL STACK ============
> Function 0: myth() @../../scripts/factorial.lua, line: 3
> Function 1: made tail call so its info is erased by Lua.
> Function 2: made tail call so its info is erased by Lua.
> Function 3: made tail call so its info is erased by Lua.
> Function 4: made tail call so its info is erased by Lua.
> Function 5: made tail call so its info is erased by Lua.
> Function 6: main() @../../scripts/factorial.lua, line: 7
> ===== END CALL STACK ============
> So, main() -- the main chunk -- called myth(), which called itself
several
> times. I had expected only 1 stack for myth().
Precisely. However, the same stack is reported five times. Lua keeps a
tail
call count, so it knows how many times to report a recycled stack.
This may or may not be useful -- I think it's a waste of cycles myself --
but it was honestly intended to help with things like debuggers. Counting
tail calls means that call hooks and return hooks are always balanced,
even if the return got turned into a tail call -- that makes life easier
for people who are trying to do things like "step through" in a debugger.
Good luck.
R.