Why apply is so important for lisp evaluator?
The eval/apply thing in SICP (and elsewhere) is separating two major parts of an evaluator. The first part, the one that eval
is doing, is dealing with the syntactic translation of code to its meaning -- but it's doing almost nothing except dispatching over the expression type. As you can see in the book, there are various eval-foo
s for various "special forms", since each of them has its own unique evaluation rule.
Now, the most important form that the evaluator needs to deal with is function application. In fact, it's so important that there is no keyword for this form (otherwise, you'd see apply
or whatever littering scheme/lisp code). Instead, if a form begins with something that is not a known special form (and in real implementations, not a known macro) then the evaluator takes it to be a function application. At this point, to evaluate a function call, you need to evaluate the function itself (the first form) and all of its arguments, and then you need to apply the first value over the rest. A major enlightenment moment here is to realize that there is a major difference between this eval
and apply
-- the former inherently deals with syntax, but the latter deals with values.
As a side note, several people confused this with the built-in apply
function that Scheme and Lisp implementation have. Why that function needs to be in the language is completely unrelated to the SICP point (roughly, it provides functionality that you cannot implement without it, it is a form of reflection from the implementation into the language). I don't even think that the SICP evaluators even make apply
available in the interpreted language. If you're looking for more enlightenment, doing that (taking a SICP meta circular evaluator, and adding apply
to the interpreted language) will be a nice exercise in reflection.
It's how you run a function on an expression, aka 'apply' the function to the expression.
Note the code here:
http://mitpress.mit.edu/sicp/code/ch4-mceval.scm (dead link)