Clojure for
uses a macro with arbitrary Clojure sequences.
These sequences may or may not be subject to random access, like vectors. Thus, in the general case, you do not have access to the last element of the Clojure sequence without going all the way to it, which would make it impossible to pass through it in the reverse order.
I assume you had something like this (Java-like pseudocode):
for(int i = n-1; i--; i<=0){ doSomething(array[i]); }
In this example, we know the size of the array n
in advance, and we can access the elements by its index. With Clojure sequences, we do not know this. In Java, it makes sense to do this with arrays and ArrayLists. Clojure sequences, however, are much more like linked lists - you have an element and a link to the following.
Btw, even if there is a way (possibly non-idiomatic) *, its temporal complexity will be something like O (n ^ 2), which is simply not worth the effort for a much simpler solution in a related record, which is O (n ^ 2) for lists and much better O (n) for vectors (and it is pretty elegant and idiomatic. In fact, the official reverse
has this implementation).
EDIT:
General advice: do not try to do imperative programming in Clojure, it was not intended for this. Although many things may seem strange or contradictory to intuition (in contrast to the well-known idioms of imperative programming), once you get used to the functional way of doing a lot of things, I mean a lot easier.
In particular, for this question, despite the same name of Java (and other C-like) for
and Clojure for
, it is not the same! The first is the actual loop - it defines flow control. The second is understanding - look at it conceptually as a higher sequence function and a function f that will be executed for each of its elements, which returns another sequence f (element) s. Java for
is an operator, it evaluates nothing, Clojure for
(like everything else in Clojure) is an expression - it evaluates the sequence f (element) s.
Probably the easiest way to get an idea is to play with the sequence function library: http://clojure.org/sequences . In addition, you can solve some problems at http://www.4clojure.com/ . The first problems are very easy, but they gradually become more complicated as you move along them.
* As shown in Alexandre, the solution to the problem is actually idiomatic and pretty smart. Kudos for that! :)