lua-users home
lua-l archive

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


It was thus said that the Great Coroutines once stated:
> 
> I always remember that local function f() ... end is equivalent to
> local f; f = function () ... end because when I have to write
> recursive functions it errors when it tries to call the
> not-yet-declared identifier from within itself.

  You should try using the Y combinator.

function Y(f)
  local function g(...) return f(g,...) end
  return g
end

print(Y(function(rec, x) 
          if x < 2 then 
            return 1 
          else 
            return x * rec(x-1)
          end
        end)(5))

  -spc (Unfortunately, do declare Y with your conding standard requres the
	use of Y ... )