Reputation: 564
In clisp, what is the difference?
(eval '(+ 1 2))
(eval (+ 1 2))
Upvotes: 2
Views: 319
Reputation: 61
All functions (except primitives and some special functions) like eval first evaluate all their argument and then pass them inside their function body.
However, one can suppress evaluation of arguments by quoting them. In such a case the S-expression itself is passed as an argument instead of it being evaluated first.
(eval (+ 1 2)) => First (+ 1 2) gets evaluated => (eval 3) => this gives answer 3
(eval '(+ 1 2)) => quote prevents argument from getting evaluated => (+ 1 2) gets passed as argument => however result of evaluating that S-expression is also 3.
The difference can be understood better from following example:
(eval (cons (+ 1 2) (+ 3 4))) => this becomes (eval (3 7)) => this gives error that "3 is not a function" as the S-expression to be evaluated is (3 7)
(eval '(cons (+ 1 2) (+ 3 4))) => this becomes like typing (cons (+ 1 2) (+ 3 4)) on REPL => evaluation of this S-expression gives result (3.7)
Upvotes: 1
Reputation: 332736
The first will pass the list (+ 1 2)
, which is similar to what you would get if you wrote (cons '+ (cons 1 (cons 2 nil))
, to the eval
function, which will then evaluate that expression, and produce the answer, 3. The expression '(+ 1 2)
is a way of quoting an expression, so that it the expression can be passed literally as data, rather than evaluated immediately. Passing it to the eval
function will then evaluate it.
The second will evaluate the expression (+ 1 2)
to get the result 3
, which is then passed to the eval
function. A number evaluates to itself, so it will return the same answer as the first case, 3.
Upvotes: 7
Reputation: 1941
It seems (eval (+ 1 2)) will first compute (+ 1 2), then use (eval 3)
(eval '(+ 1 2)) will transfer expression (+ 1 2) to eval, and let eval to interpret it.
Upvotes: 3