lua-users home
lua-l archive

Re: Recursive Anonymous Functions

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


On Wednesday, August 01, 2007 Patrick Donnelly wrote:
> I know this has been talked about before, and I read all the posts
> in the archive on this. There wasn't an "accepted" method for going
> about it, so I wondered if this implementation is optimal: [...]
Not sure what your definition of optimal is, but here are some
other options...
-- without execute()
function test1(s)
 return (function(f)
 local fact;
 fact = function(n, ret)
 ret = ret or 1;
 if n == 0 then return 1 * ret end;
 return fact(n - 1, n * ret);
 end;
 return fact(f);
 end)(s);
end;
-- without 'local fact'
function recur (f,...)
 return f(f,...);
end;
function test2(s)
 return recur(function(fact,n,ret)
 ret = ret or 1;
 if n == 0 then return 1 * ret end;
 return fact(fact, n - 1, n * ret);
 end,s);
end;
-- e
-- 
Doug Currie
Londonderry, NH, USA

AltStyle によって変換されたページ (->オリジナル) /