lua-users home
lua-l archive

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



On 22-Jul-04, at 2:49 PM, Aaron Brown wrote:

I don't know how deep you'd have to get into the language in
question to answer this, but wouldn't the function be
considered non-anonymous if a variable contained a reference
to it?

I suppose we could distinguish between anonymous functions and
pronominal functions. The use case, in some hypothetical language,
would be something like this:

factorials = map(function(i)
                   if i > 1 then return i * me(i - 1)
                            else return 1
                   end
                 end,
                 myList)

where me is a pronominal reference to the enclosing function.

I'd be inclined to say that this style of programming is not particularly readable, but others may disagree :) In any event,
the Lua idiom would be:

do
  local function fact(i) ... end
  factorials = map(fact, myList)
end

<digression for="Bennett">
  This is exactly equivalent to:

  do
    local fact
    fact = function(i) ... end
    factorials = map(fact, myList)
  end
</digression>

Although some might call such a function "anonymous", this doesn't
seem to capture Lua's semantics, In some sense, all Lua objects are
anonymous (in the sense that no Lua object has an intrinsic name),
But one could say that a function was anonymous in a compilation unit if it had no named reference visible in that compilation unit, which would apply, for example, to:

  map(function(i) return i + 1 end, myList)

Clearly, if a function is going to call itself it needs to have a named reference to itself, so no recursive function could truly be anonymous in that sense; however, the "me" formulation above might be considered a halfway house -- hence "pronominal"

The infamous Y combinator is another halfway house, in the sense that the function happens to be recursive but doesn't really know that it is.

R