What is wrong with this macro in Clojure?

(defmacro nif [expr pos zer neg]
  '(condp = (Integer/signum ~expr) 
     -1 ~neg
     0 ~zer
     1 ~pos))

I get this error.

1:1 user=> #<Namespace Chapter7Macros>
1:2 Chapter7Macros=> (nif 1 (+ 2 2) (- 2 2) (- 3 2))
1:3 Chapter7Macros=> java.lang.Exception: Unable to resolve symbol: expr in this context (repl-1:57)
+3
source share
2 answers

Replace the quote (') with the back (`) to enable syntax quoting.

+6
source

In general, using (macroexpand-1 '(nif 1 ...)) will help you by showing you the code that your macro actually translates to.

+3
source

Source: https://habr.com/ru/post/1713515/


All Articles