Wolfgang says:
> On the other hand, this seems to imply that function arrays can't be
> used to manipulate global bindings. Hmm...
No, that's not true. Your example works the way it does because of the
double evaluation.
> So is this a bug or should we add some warning to the manual?
No, not a bug. But I agree that the semantics of indexed functions are
confusing, and deserve a clear explanation. Currently, I think they
have no explanation at all....
The body of the function is evaluated at TWO times. Your example works
correctly; it is not a bug. But the double evaluation does have
confusing consequences.
The following shows what is going on:
(C1) f[i](x):=[argvals:cons([i,x],argvals),
'(qargvals:cons([i,x],qargvals))]$
/* The first part is evaluated when f[i] is calculated;
the second (quoted) part only when f[i] is applied to x. */
(C2) (argvals:[], qargvals: [])$
(C3) f[2](3);
(D3) [[[2, 3]], [[i, 3]]]
(C4) f[2](4);
(D4) [[[2, 4]], [[i, 4], [i, 3]]]
/* f[2] has already been evaluated once, so it is not reevaluated.
Why 4 instead of 3, then? Let's look at f[2] */
(C5) f[2];
(D5) LAMBDA([x], [[[2, x]], qargvals : CONS([i, x], qargvals)])
/* Aha! There's still the unevaluated 'x' there -- remember, f[2]
must be evaluated in a context where x is not yet bound */
(C6) f[5](6);
(D6) [[[5, 6], [2, 6]], [[i, 6], [i, 4], [i, 3]]]
(C7) f[5](7);
(D7) [[[5, 7], [2, 7]], [[i, 7], [i, 6], [i, 4], [i, 3]]]
(C8) f[r](8);
(D8) [[[r, 8], [5, 8], [2, 8]], [[i, 8], [i, 7], [i, 6], [i, 4], [i,
3]]]
(C9) f[r];
(D9) LAMBDA([x], [[[r, x], [5, x], [2, x]], qargvals : CONS([i, x],
qargvals)])
(C10) argvals;
(D10) [[r, x], [5, x], [2, x]]
(C11) qargvals;
(D11) [[i, 8], [i, 7], [i, 6], [i, 4], [i, 3]]