Continuing in learning clojure, today we’re going to look at looping constructs.
The standard looping construct in clojure is loop
and recur
. It’s super easy and does all your fancy tail call optimization stuff.
The format of loop
is (loop [bindings*] exprs*)
; all that means is that it takes a list of variable/value bindings and it has a function body.
recur
works hand in hand with loop
–it passes back into the loop new values for the loop
binding arguments.
Example:
For “range-y” style looping, dotimes
is a great solution. You simply pass in a single binding of how many times you want it to loop:
While is a strange looping construct in clojure (IMO) b/c it expects its test expression to change at some point.
That’s it!