lua-users home
lua-l archive

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


Dear All,

With the intention of announcing a positive result, I just tried
Knuth's "man or boy" test from ye olde Algol days with
Lua, but my test program doesn't return the correct -67
but rather -299.

As far as I understand it, the test checks whether closures
are still working under recursion.

I modeled the program closely after the JavaScript one, which
itself was taken from the Wikipedia article (with minor
modifications).

http://en.wikipedia.org/wiki/Man_or_boy_test

I append both at the bottom of the mail.

Did I miss something when writing the Lua test?

*Should* Lua return the -67 result or has it different
closure semantics for some valid reason?

Regards,
Peter

Lua:

--- begin of manorboy.lua ---
function A (k, x1, x2, x3, x4, x5)
    function B ()
        k = k - 1
        return A (k, B, x1, x2, x3, x4)
    end
    if k > 0 then
        return B ()
    else
        return x4 () + x5()
    end
end

print (A (
    10,
    function () return 1 end,
    function () return -1 end,
    function () return -1 end,
    function () return 1 end,
    function () return 0 end
))
--- end of manorboy.lua ---

JavaScript

---begin of manorboy.js ---
function A (k, x1, x2, x3, x4, x5) {
    function B () {
        k -= 1;
        return A (k, B, x1, x2, x3, x4);
    }
    if (k > 0) {
        return B ();
    } else {
        return x4 () + x5 ();
    }
}

result = A (
    10,
    function () {return 1},
    function () {return -1},
    function () {return -1},
    function () {return 1},
    function () {return 0}
)

WScript.Echo ("Result: " + result)
--- end of manorboy.js ---