gatesphere wrote:I've traced it, and found that after a (sum 4), sum => (lambda ((x 0)) (inc 4 x))... this confuses me.
Consider the syntax diagram for how the expression "
(inc 0 x)" might be stored in memory.
The 'inc' primitive, when invoked with its first argument being a literal number, apparently behaves by updating the memory cell of that atomic number (represented in the diagram by the oval containing a blue "
0"). If the expression, "
(inc 0 x)" is evaluated by itself on the command line, the "
0" cell is updated but then destroyed when the function is completed (in Scheme the cell would be released for later garbage collecting, but my understanding is that Newlisp does not produce such garbage).
However, since in your example "
(inc 0 x)" is contained within the lambda definition of 'sum', the "
0" cell (indeed the entire lambda expression) persists in memory. So when you execute "
(sum 4)", the "
0" cell is updated to contain "
4", which continues to persist within the definition of 'sum'.
That would be my characterization of what is occurring -- the "magic" of the function modifying itself takes place inherently within the execution of 'inc', not through any explicit manipulation of the
AST of 'sum'. I could, of course, be completely off-base (the documentation for 'inc' does not seem to cover in detail the case of the 'place' parameter being a literal number).