blue note
blue note

Reputation: 29081

clojure macro: unable to resolve symbol

Clojure beginner, trying macros. I'm writing the following macro

(defmacro f [exp]
  (let [[a op b] exp]
    (list op a b)))

(f (1 + 2))

which works as intended.

However

I tried to replace the returned value from (list op a b) to '(op a b) and I get *unable to resolve symbol op in this context. I figured that the error is caused because list evaluates its arguments first, so I tried with '(~op a b), but still get the same error. What am I understanding wrong?

Upvotes: 2

Views: 763

Answers (1)

erdos
erdos

Reputation: 3538

The problem is that op, a, b can not be evaluated inside a quoted form. You need to use the backtick symbol instead of ' (single quote) if you want to use ~ (unquote) inside a macro.

(defmacro f [exp]
  (let [[a op b] exp]
    `(~op ~a ~b)))

Upvotes: 4

Related Questions