manorboy.lua 671 B

123456789101112131415161718192021222324
  1. -- The man or boy test was proposed by computer scientist Donald Knuth as a
  2. -- means of evaluating implementations of the ALGOL 60 programming language.
  3. -- The aim of the test was to distinguish compilers that correctly implemented
  4. -- "recursion and non-local references" from those that did not.
  5. -- https://rosettacode.org/wiki/Man_or_boy_test#Lua
  6. local function a(k,x1,x2,x3,x4,x5)
  7. local function b()
  8. k = k - 1
  9. return a(k,b,x1,x2,x3,x4)
  10. end
  11. if k <= 0 then return x4() + x5() else return b() end
  12. end
  13. local function K(n)
  14. return function()
  15. return n
  16. end
  17. end
  18. return function(N)
  19. N = N or 10
  20. print( a(N, K(1), K(-1), K(-1), K(1), K(0)) )
  21. end