Reputation: 49
I an exercise I have the following function :
(defun ifnot (test e1 e2)
(if test e2 e1))
I'm asked to describe the evaluation of the following expression :
(ifnot nil "OK" (error "Unexpected test outcome"))
I don't know if the problem is about the token "nil" or about the builtin error, may you help me ?
Upvotes: 0
Views: 62
Reputation: 5241
ifnot
, as you've written it, is a function. When a function is evaluated, all of the arguments are evaluated before the function body is evaluated. While the value of e2
is thrown away in your example, e2
is still evaluated, and so error
is still called. If it were a macro instead, then it would work as you seem to intend:
(defmacro ifnot (test e1 e2)
`(if ,test ,e2 ,e1))
This way, the code
(ifnot nil "OK" (error "Unexpected test outcome"))
is replaced with this at compile time:
(if nil (error "Unexpected test outcome") "OK")
Upvotes: 2