for
is a list comprehension operator, its return value is a list. The return value of println
is always nil
, and those are the values that for
is using to build the list it returns. Since you are (println n)
, your prints interleave with the REPL's printing of the return value for the for
.
If you avoid the use of println
in the for
, the REPL will show you the list of numbers from 0 to 39:
(defn my-for []
(for [n (range 0 40)] n))
(my-for)
;= (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39)
If you only want to print the values then you can use the doseq
form, which is used for side-effecting functions (such as println
) and always returns nil
:
(defn my-doseq []
(doseq [n (range 0 40)]
(println n)))
(my-doseq)