In Common Lisp, I can define and use a macro that captures a variable from lexical scope, so that I can use it within a let
form:
CL-USER> (defmacro plus9 () `(+ 9 woo))
PLUS9
CL-USER> (macroexpand '(plus9))
(+ 9 WOO)
T
CL-USER> (let ((woo 1)) (plus9))
10
However, when I define a similar macro in Clojure, it's looking for a definition of woo
in the user
namespace:
user=> (defmacro plus9 [] `(+ 9 woo))
#'user/plus9
user=> (macroexpand '(plus9))
(clojure.core/+ 9 user/woo)
user=> (let [woo 1] (plus9))
CompilerException java.lang.RuntimeException: No such var: user/woo
Clearly I'm misunderstanding how scopes work in Clojure. What's the correct way of achieving this macro with Clojure?
I'm a macro noob, but how about
user=> (defmacro plus9 [] `(+ 9 ~'woo))
#'user/plus9
user=> (macroexpand '(plus9))
(clojure.core/+ 9 woo)
user=> (let [woo 1] (plus9))
10
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