The (println x)
forms in your examples are side-effects which will only occur if the for
lazy sequence is realized.
Here is the first example reformatted:
(do
(println "ok")
(for [x [1 2 3]]
(println x)))
do
evaluates the expressions in turn and returns the value of the last one. Here the last expression is a lazy sequence. If you evaluated this in a REPL, the lazy sequence will be realized to print its elements (giving value (nil nil nil)
) and cause the side-effects of printing 1
2
3
.
Here is the second example reformatted:
(do
(for [x [1 2 3]]
(println x))
(println "ok"))
Again do
evaluates the expressions in turn. The first one is a lazy sequence but its value is not used and so is never realized. This means the side-effects of printing 1
2
3
don't occur.