Can someone explain me with some example the difference beteen Apply and Eval in LISP? I can't understand it.
EVAL executes arbitrary source code made of Lisp's data structures. This includes function calls, macro forms, special forms, variables and self-evaluating data.
APPLY applies a function to a list of arguments.
What better way to understand it, than to take a look at the actual implementation! See the Metacircular Evaluator section in SICP, it has a very clear explanation on the inner workings of both eval
and apply
. Quoting from the book:
Eval takes as arguments an expression and an environment. It classifies the expression and directs its evaluation. Eval is structured as a case analysis of the syntactic type of the expression to be evaluated. In order to keep the procedure general, we express the determination of the type of an expression abstractly, making no commitment to any particular representation for the various types of expressions. Each type of expression has a predicate that tests for it and an abstract means for selecting its parts. This abstract syntax makes it easy to see how we can change the syntax of the language by using the same evaluator, but with a different collection of syntax procedures.
Apply takes two arguments, a procedure and a list of arguments to which the procedure should be applied. Apply classifies procedures into two kinds: It calls apply-primitive-procedure to apply primitives; it applies compound procedures by sequentially evaluating the expressions that make up the body of the procedure. The environment for the evaluation of the body of a compound procedure is constructed by extending the base environment carried by the procedure to include a frame that binds the parameters of the procedure to the arguments to which the procedure is to be applied.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With