for
The for loop is a common concept in computer languages.
When we want to apply the same logic (operations) to each element of a given array (vector or list in Clojure),
we apply the idea of a for loop to that.
In general, this involves incrementing (or decrementing) an index and performing a similar function on each element.
However, the way to implement a for loop varies from language to language.
Some use an index variable explicitly. Others use an iterator. Some use neither.
Clojure takes a very different approach.
Clojure’s for
is categorized as a sequence operator, like map
(core function) or
reduce
.
More importantly, in Clojure, for
is used for list comprehension, which means it creates a list from a given list.
The syntax of the for
macro is:
(for [binding-form coll-expr filter-expr?] expr)
Try some examples.
ClojureDocs
Clojure from the ground up: macros, List comprehensions
http://aphyr.com/posts/305-clojure-from-the-ground-up-macros
Introdcution to Clojure, Bread and Butter Functions
http://clojure-doc.org/articles/tutorials/introduction.html#bread-and-butter-functions