lua-users home
lua-l archive

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


In PiL ed 3, Section 6.3 on Proper Tail Calls it says the only calls that follow the following form are proper tail calls:

return func(args)

Would the following statements fit that form or not?

local function _map(func, key, tbl)   
    local nextKey = next(tbl, key)
    if not nextKey then
        return
    else           
        return func(tbl[nextKey]), _map(func, nextKey, tbl)
    end
end

function Map(func, ...)
    return _map(func, nil, {...})
end

It seems like I can Map >100,000 arguments without any stack overflow exceptions or increases in memory. So it seems like it is doing a tail call elimination. Is this because the

func(tbl[nextKey])

is adjusted to a single return before the _map() tail call begins? So there is nothing left for the original _map call to do, so the new _map() tail call reuses its stack?

Take Care,
    Derek