How do I print a list of numbers on each line in clojure?
Asked Answered
C

5

13

how can I print a list of n, say 10, numbers on 10 lines? I just learned about loop and recur, but cannot seem to combine a side-effect (println i) with (recur (+ i 1)) in a loop form. Just to be very clear: I'd like output like this:

1
2
3
4
5
6
7
8
9
10

when n is 10.

Cribbing answered 29/6, 2011 at 12:34 Comment(0)
N
16

You can use doseq for this, which is meant to be used when iteration involves side effects,

(doseq [i (range 10)]
   (println i))

You could use map as pointed but that will produce a sequence full of nils which is both not idiomatic and wastes resources also doseq is not lazy so no need to force it with doall.

Nisus answered 29/6, 2011 at 17:5 Comment(2)
This prints number 0 to 9; using (inc i) in the println, as shown in the other answers below, gives 1 to 10.Cribbing
instead of inc just do (range 1 11)Nisus
V
11

I suggest dotimes for this kind of simple loop:

(dotimes [i 10]
  (println (inc i)))

Note that dotimes is non-lazy, so it is good for things like println that cause side effects.

Vorous answered 29/6, 2011 at 17:35 Comment(0)
T
3

With loop/recur:

(loop [i 1]
  (when (<= i 10)
    (println i)
    (recur (inc i))))

However, it's more idiomatic (read: more "Clojuristic") to map the function println over the numbers in 1..10. But because map returns a lazy sequence, you must force its evaluation with doall:

(doall (map println (range 1 (inc 10))))
Trimly answered 29/6, 2011 at 12:41 Comment(2)
In fact it is more idiomatic to use doseq (or dotimes) in this case, because it is all about side-effects. map should only be used if you are interested in the return value. Otherwise you get easily caught by laziness.Bengali
So that would be (dotimes [i 10] (println i)) Thanks! Will remember that when I get to chapter 14 in 'Practical Clojure' (now finishing up chapter 3) :-)Cribbing
F
0

And just to be comprehensive you can do it with map also:

(doseq (map #(println %) (range 10))
Friable answered 29/6, 2011 at 17:34 Comment(0)
D
0

If you only want to print the output on the screen, you might also simply put a (println i) before entering your conditional:

(loop [i 0]
  (println i)
  (if (< i 10)
    (recur (inc i))
    (println "done!")))

And the output will be one number per line.

Downing answered 17/7, 2015 at 0:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.