• Subject: Lua fails Knuth's "man or boy" test?!
• From: "Peter Jacobi" <pjacobi.de@...>
• Date: Sat, 1 Nov 2008 22:21:10 +0100

```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 ---

```