Reputation: 21615
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.
Upvotes: 7
Views: 2207
Reputation: 58558
It isn't universal. For instance, it assumes strict semantics, whereas languages can be lazily evaluated.
However, the Lisp-based representation of the concept eliminates most issues of syntax, so that there are few distractions away from the bare semantics.
In order to talk about how some program is structured and interpreted, we need to refer to its syntax. If there is a distinct outer syntax which is visible, and an invisible inner syntax, the description becomes muddled. Lisp makes them almost one and the same. Plus, the processing itself is expressible in the same Lisp. We can easily talk about the interpretation of the syntax, since it is laid bare, and we have names for all the parts. Moreover, we can write the processing in that same syntax.
Upvotes: 0
Reputation: 40309
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)
Upvotes: 1
Reputation: 29546
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.
Upvotes: 8
Reputation: 6346
Apply evaluates a function call. It takes a function and a list of arguments: (apply fn args)
. If you have a language that supports function calls, you're probably going to have an apply function in your interpreter. The difference between Scheme and Javascript is that Scheme exposes this function not only to the interpreter, but also to the program that is being interpreted.
Upvotes: 1