Unbound variable error when using defmacro in LISP -
i trying use macro 2 forms in lisp, evaluates both forms return result of form 2. below code using -
(defmacro testmac (x body) (prog2 x body))
when executing macro following forms, works correctly , return 5 second form.
(testmac (- 10 6) (/ 10 2))
however when try execute macro following forms, return error.
(testmac (print a) (print b))
below error -
debugger invoked on unbound-variable: variable b unbound. type debugger help, or (sb-ext:exit) exit sbcl. restarts (invokable number or possibly-abbreviated name): 0: [abort] exit debugger, returning top level. (sb-int:simple-eval-in-lexenv b #<null-lexenv>)
why getting error , how can use macro make work?
p.s. cannot use defun need use macro execute (testmac (print a) (print b))
i trying use macro 2 forms in lisp, evaluates both forms return result of form 2.
that's not idea - though might not precise wording. macro should not evaluate code - not without reason. macro transforms code. generated code decides evaluate.
(defmacro testmac (x body) (prog2 x body)) (testmac (- 10 6) (/ 10 2))
so x
list (- 10 6)
, body list (/ 10 2)
.
your macro returns second list.
cl-user 11 > (macroexpand-1 '(testmac (print a) (print b))) (print b)
the macro returns form (print b)
. gets executed.
cl-user 12 > (testmac (print a) (print b)) error: variable b unbound.
if b
undefined error see.
there no magic going on.
Comments
Post a Comment