philipbohun.com

Glojure by Example: Loop and Recur

loop and recur are the general repetition form. loop introduces bindings. recur jumps back to the loop with new values, and it must be in tail position. This loop adds the numbers below 4.

(println
  (loop [i 0
         acc 0]
    (if (= i 4)
      acc
      (recur (inc i) (+ acc i)))))

dotimes repeats a body a fixed number of times. doseq walks a collection for side effects. Neither form builds a return value. The sequence page shows map and for, which do.

(dotimes [i 3]
  (println "dotimes" i))

(doseq [color [:red :blue]]
  (println "doseq" color))

The loop returns 0+1+2.

$ glj loop-and-recur.glj
6
dotimes 0
dotimes 1
dotimes 2
doseq :red
doseq :blue

Next example: Vectors.