loop
The loop
special form is not like a for loop.
The usage of loop
is the same as the let
binding.
However, loop
sets a recursion point.
The recursion point is designed to use with recur
,
which means loop
is always used with recur
.
To make a loop happen, the number of arguments (arity) specified for recur
’s must coincide with the number of bindings for loop
. That way, recur
goes back to loop
.
The syntax is: (loop [bindings*] exprs*)
Compare the example above with the one in recur
.
For a loop exercise, let’s think about how to calculate the sum of a geometric series (http://en.wikipedia.org/wiki/Geometric_series. A well-known geometric series is 1/2 + 1/4 + 1/8 + 1/16 + …., which converges to 1.
ClojureDocs
Clojure.org, Special Forms, loop
http://clojure.org/special_forms#Special%20Forms–(loop%20[bindings%20]%20exprs
Introduction to Clojure, Looping and Recursion
http://clojure-doc.org/articles/tutorials/introduction.html#looping-and-recursion
Clojure from the ground up: macros, Recursion
http://aphyr.com/posts/305-clojure-from-the-ground-up-macros
Clojure for the Brave and True, Do Things, 4.3. loop
GetClojure