lispsicp

Why apply is so important for lisp evaluator?


I have read chapter 4 of SICP, and just found that the first section lists the most important functions for implementing a evaluator, eval and apply, I understand that eval is very important, but why apply is so important? For some language, there is totally no apply such as in Javascript.

Edit: Sorry about that I am wrong about there is no apply in Javascript, please just ignore that.


Solution

  • 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-foos 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.