lua-users home
lua-l archive

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

On 8/23/05, Brent Arias <> wrote:
> Ok, yes, I figured that was what was happening.  However, I don't understand
> why.  What inspired Lua, fortunately, to not evaluate the perm{"a", "b",
> "c"} expression over and over again?
> Knowing the answer, namely of Lua's calling logic, would make me a better
> overall user of lua.

I think perhaps the confusion here relates to the difference between a
C loop construct and a Lua loop construct. In C, of course, the test
for a for-loop is an expression that is evaluated before each
iteration through the loop. C doesn't have any concept of a lambda, so
that's really the only way to do it. The entirety of the loop-test
expression, in essence, is treated as a lambda.

Lua, however, does have the luxury of lamba functions. Instead of
evaluating an expression for each iteration, it calls the lambda
function it has been given. So when you think about a line like
'perm{"a", "b", "c"}', realize that the fact that a function call is
happening at all, is a red herring. There doesn't need to be a
function being CALLED, only a function RESULTING from the evaluation
of the expression. Evaluating the expression, in other words, is not a
part of iteration. It's as though you did something like "local x = 2
+ 3"... the VM does not add 2 and 3 each time you refer to x.