Why isn't my code printing like it should?

后端 未结 3 2035
伪装坚强ぢ
伪装坚强ぢ 2021-01-18 03:51

I\'m trying to create a for in Clojure.

I\'m following the cheats sheet from the Clojure site.

e.g:

(take 100 (for [x (range 100000000) y (ra         


        
3条回答
  •  Happy的楠姐
    2021-01-18 04:20

    Actually, it is working for me. The last example does print one hundred "Hello World", but the take gets just one hundred nils that are also printed in the REPL. The first example works as it is:

    user=> (take 100 (for [x (range 100000000) y (range 1000000) :while (< y x)] [x y])) ([1 0] [2 0] [2 1] [3 0] [3 1] [3 2] [4 0] [4 1] [4 2] [4 3] [5 0] [5 1] [5 2] [5 3] [5 4] [6 0] [6 1] [6 2] [6 3] [6 4] [6 5] [7 0] [7 1] [7 2] [7 3] [7 4] [7 5] [7 6] [8 0] [8 1] [8 2] [8 3] [8 4] [8 5] [8 6] [8 7] [9 0] [9 1] [9 2] [9 3] [9 4] [9 5] [9 6] [9 7] [9 8] [10 0] [10 1] [10 2] [10 3] [10 4] [10 5] [10 6] [10 7] [10 8] [10 9] [11 0] [11 1] [11 2] [11 3] [11 4] [11 5] [11 6] [11 7] [11 8] [11 9] [11 10] [12 0] [12 1] [12 2] [12 3] [12 4] [12 5] [12 6] [12 7] [12 8] [12 9] [12 10] [12 11] [13 0] [13 1] [13 2] [13 3] [13 4] [13 5] [13 6] [13 7] [13 8] [13 9] [13 10] [13 11] [13 12] [14 0] [14 1] [14 2] [14 3] [14 4] [14 5] [14 6] [14 7] [14 8])

    For actually printing, you can try a more easy loop:

    (dotimes [i 100] (println "Hello World"))
    

    Finally, if you use for, you don't need the :while, as the range already has 100 elements:

    (take 100 (for [a (range 100)] 
                   (println "Hello World")))
    

提交回复
热议问题