1

I want to program a macro in clojure. The macro is called like this (FORI from to task) e.g. (FORI 1 10 (println i)) and should print the numbers from 1 to 10. So far i have

(defmacro FORI [from to task]
  `(let [i# (range ~from ~to)]
     ~task))

I don´t know how do get the list i# into the (print i).

Thanks for help.

1

1 Answer 1

2

You can use any symbol in hygienic forms if you unquote their quoted form:

(defmacro fori [from to task]
  `(doseq [~'i (range ~from ~to)]
      ~task))

So here in the expanded form, symbol i will be substituted without change.

Note, using fixed symbols in macros is usually not a good practice, because now i will shadow other vars with the same name.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much :) Macros are hard for me as a clojure beginner.

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.