lua-users home
lua-l archive

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


Were you look for tail-call optimization and surprised not to see it? Try:

    p2 = function()
      return p3()
    end

    p1 = function()
        return p2()
    end

Without the return, the function needs to reduce the number of results to
zero after making the call, so it isn't done and hence can't use a tail
call.

Mark  

on 9/29/04 6:16 AM, Frederico Rodrigues Abraham at devotion97@gmail.com
wrote:

> Hi.
> I noticed something that seems strange to me when using the error
> function with an error level parameter.
> Consider the following piece of code:
> 
> p3 = function()
> error("the error",3)
> end
> 
> p2 = function()
> p3()
> end
> 
> p1 = function()
> p2()
> end
> 
> luaf = function(d)
> print(d)
> p1()
> return "the result"
> end
> 
> print(luaf("ha!"))
> -- END OF CODE
> 
> Running the lua 5 interpreter with this file gives the following stack trace:
> 
> lua5: stacktrace.lua:10: the error
> stack traceback:
>       [C]: in function `error'
>       stacktrace.lua:2: in function `p3'
>       stacktrace.lua:6: in function `p2'
>       stacktrace.lua:10: in function `p1'
>       stacktrace.lua:15: in function `luaf'
>       stacktrace.lua:19: in main chunk
>       [C]: ?
> 
> The line where the error happened seems correct. (line 10, the call to
> p2() in p1 body)
> Shouldn't the stack trace omit the lines for error levels 1 and 2?
> (line 2 and 6, respectively)
> 
> -- Fred