lua-users home
lua-l archive

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


Gabor Grothendieck <ggrothendieck <at> myway.com> writes:

: 
: Philip Bock <phil <at> flamewars.org> writes:
: 
: > Is there any way for an anonymous function to call itself?
: 
: I know of one language that has a built in Recall function that 
: will recursively call the function its called in to make it
: easier to write anonymous recusrive functions.  I don't think Lua 
: has such a facility but if it does someone might be able to point it 
: out.

I've done a bit of reading on Lua since my first post and 
it seems it is possible to define Recall using the
facilities already present in Lua.  Note that Recall is
what some other posters referred to as me.

For example, we can invoke the anonymous factorial function 
evaluating it for the argument 3 like this:

   Recall = function(...) return debug.getinfo(2,"f").func(unpack(arg)) end

   print((function(x)
      if x == 0 then
         return 1
      else
         return x * Recall(x-1)
      end
   end)(3))