lua-users home
lua-l archive

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


> local function Y(f)
> local function g(...) return f(g, ...) end
> return g
> end
> 
> print(Y(function(fac, n) if n <= 1 then return 1 else return n*fac(n-1) end
> end)(10))
> 
> 
> There's a more complicated version of Y that does not require an assignment
> (local function g(...)  is really local g; g = function(...))

The problem here is not the assignment per se, but the fact that 'g'
is recursive, so it only moves the recursion from one place to another.
The following version does not use recursion (although this particular
one is usually called Z, not Y):

local Y = function (le)
      local a = function (f)
        return le(function (...) return f(f)(...) end)
      end
      return a(a)
    end


-- Roberto