Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why apply is so important for lisp evaluator?

Tags:

lisp

sicp

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.

like image 888
Thomson Avatar asked Jun 07 '11 16:06

Thomson


Video Answer


3 Answers

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.

like image 171
Eli Barzilay Avatar answered Nov 11 '22 06:11

Eli Barzilay


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)

like image 41
Paul Nathan Avatar answered Nov 11 '22 07:11

Paul Nathan


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.

like image 27
Jules Avatar answered Nov 11 '22 08:11

Jules