lua-users home
lua-l archive

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


Hello,

I'm experiencing a problem that seems to have to do with nested
functions that use recursion on both of the levels of the nesting.

I've boiled the problematic code down to the following simple
example which demonstrates the problem:


function x(q)

    local y = ""

    function z(r)

        y = y .. "#" .. r .. " "

        if r > 0 then
--          print("inner x(0) = " .. x(0))
            print("inner y = " .. y)
            z(r-1)
        end
    end

    z(q)
    y = y .. "end."

    return y
end

print("x(0) = " .. x(0) .. "\n")
print("x(1) = " .. x(1) .. "\n")
print("x(2) = " .. x(2) .. "\n")



As it is, with the inner x(0) function call commented
out, this program produces the following result as
expected:

x(0) = #0 end.

inner y = #1 
x(1) = #1 #0 end.

inner y = #2 
inner y = #2 #1 
x(2) = #2 #1 #0 end.


However, when the line is commented in, the inner
function computes the value of x(0). This results in
the following output:

x(0) = #0 end.

inner x(0) = #0 end.
inner y = #1 
x(1) = #1 end.

inner x(0) = #0 end.
inner y = #2 
inner x(0) = #0 end.
inner y = #0 end.#1 
x(2) = #2 end.


Notice how, in the invocation of x(2), the inner function's
value of y is first shown correctly as "#2 ", but then gets
changed mysteriously to "#0 end." after computing that result
for x(0). However, x(0) ought have to been computed in a
completely different activation frame and thus have no effect
on the value of y in the invocation of x(2).

Also, when the "end." is concatenated in the invocation of x(2),
the value of y was only "#2" and did not reflect what happened
in the recursion of the inner function, not even the erroneous
result.

I've observed this effect in both Lua 5.0.1 and Lua 5.0.2.

I'm a bit puzzled as to what to make of these results. Any
ideas would be greatly appreciated!

Thanks,
Eric