1

Consider this minimal example

(defmacro foo []
  `(list ,@(for [i [1 2 3]] (+ 1 i))))

I expect (foo) to expand to (list 2 3 4)

But instead, I get a bunch of indecipherable errors when I try (macroexpand-1 (foo)

Syntax error macroexpanding clojure.core/let at (*cider-repl <filepath>:localhost:43203(clj)*:366:22).
test/i - failed: simple-symbol? at: [:bindings :form :local-symbol] spec: :clojure.core.specs.alpha/local-name
test/i - failed: vector? at: [:bindings :form :seq-destructure] spec: :clojure.core.specs.alpha/seq-binding-form
test/i - failed: map? at: [:bindings :form :map-destructure] spec: :clojure.core.specs.alpha/map-bindings
test/i - failed: map? at: [:bindings :form :map-destructure] spec: :clojure.core.specs.alpha/map-special-binding

What's going wrong? Can't I use for loops inside my macro definitions?

1 Answer 1

3

,@ should be ~@.

user=> (defmacro foo []
  `(list ~@(for [i [1 2 3]] (+ 1 i))))
#'user/foo

user=> (macroexpand-1 '(foo))
(clojure.core/list 2 3 4)

user=> (foo)
(2 3 4)
Sign up to request clarification or add additional context in comments.

1 Comment

The # is unrelated. There's no qualifying of variables needed here, since they're not introduced into the caller's scope. You've in fact just named a variable i#, which is legal but confusing. The only important change is the switch from , to ~.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.