lua-users home
lua-l archive

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


> do  local fact
>     fact = function(x) if x<2 then return 1 else return x*fact(x-1) end end
>     fac = fact
> end

More Lua like is:

do
  local function fact(x) ... end
  fac = fact
end

I think there was also some discussion a long time ago to do it
something like this:

-- generic helper function to create anonymous recursive functions
function Y(f)
  local function me(...) return f(me, ...) end
  return me
end

-- the prototypical example..
fac = Y(function(me, n)
  if n == 0 then return 1
  else return n*me(n - 1)
  end
end)

Bye,
Wim