Clojure's for
macro accepts two arguments: a sequence of binding forms, and a body expression. As such, if I want to do multiple things in the loop, I have to wrap multiple expressions in a do
block to make them a single expression.
Compare:
(doseq [a (range 3)]
(prn 'a a)
(inc a))
to:
(for [a (range 3)]
(prn 'a a)
(inc a))
The doseq
works as expected. The for
complains:
clojure.lang.ArityException: Wrong number of args (3) passed to: core/for
My question is, why doesn't this work? Why didn't the designer(s) of Clojure allow multiple "body" expressions in the for loop, like they did in doseq
and when
? It's not as if there is any semantic ambiguity, right?