In Clojure and most other Lisps, `if' has the following structure -
(if test
then
else)
If you want to have multiple forms in the `then' or `else' clause, you
have to group them together, else it will become a part of the `then'
clause (or will throw an error since if can take only three args).
This also tells you that you are performing a side-effect since (do
foo bar) will return the return value of bar and that of foo will be
completely discarded.
Coming back to your example, if you drop the do, then only the println
part will be the `then' clause and the recur part will become the
'else' clause and will get executed only when the test is false.
This will make the function print the first item and exit.
I hope that was clear enough.
Regards,
BG
--
Baishampayan Ghose
b.ghose at gmail.com
(when (not (empty? s)
(println (str "Item: " (first s)))
(recur (rest s))))
As there is only one case for a when, you can give multiple
instructions for this case without grouping them
Even better:
(when-not (empty? s)
Good tips! Another option:
(defn printall [s]
(doseq [i s]
(println "Item:" i)))
--Chouser
http://joyofclojure.com/