It is easy to apply multiple nested for loop inside a while loop in Python. However, it is not the case in Clojure. For my understanding, "for", "while" or "loop"; all of them are macro. Please comment how to write the looping with idiomatic Clojure code ?
n = 1
while n < 10:
for i in range(10):
print("for loop 1")
for j in range(10):
print("for loop 2")
print("back to loop")
for j in range(10):
print("loop3")
print("back to loop again")
n += 1
for loop does not execute, how to write idiomatic Clojure code?
(defn Example []
(def n (atom 1))
(while (< @n 10)
(do
(println @n)
;; for loop does not execute
(for [i (range 10)]
(println "for loop 1")
;; for loop does not execute
(for [j (range 10)] (println "for loop 2")))
(println "back to loop")
;; for loop does not execute
(for [j (range 10)] (println "for loop 3"))
(println "back to loop again")
(swap! n inc))))
(Example)
defon namespace level. Useletinstead. And even better don't use atoms to accumulate some state - most likely loop/recur is what you are after here.