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 Miles Bader once stated:
> Dirk Laurie <dirk.laurie@gmail.com> writes:
> > How many characters do you save?
> >
> >     local function func(x,y)
> >     local func=function(x,y)
> >
> > Zero. That's right, nada, nichts, rien, zilch.
> 
> ... unless the function is recursive, in which case, a fair number.
> 
> [And self-recursive functions are fairly common...]

  And that's why you have 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 (Now all we need is call/cc ... 8-P