In addition to Scott's answer :
The reverse function decreases its sequence argument, element by element. If we can make reverse really lazy, we can do the same with other abbreviations. So write a lazy version of reduce :
(defn lazy-reduce [f init coll] (lazy-seq (if (seq coll) (lazy-reduce f (f init (first coll)) (rest coll)) init)))
This, by the way, only works with sequences.
But it works:
(lazy-reduce conj () (range 5)) ; (4 3 2 1 0)
If we never touch the result, nothing is calculated. We are winning. But as soon as we touch the first element, the whole sequence is realized, unraveling, like knitting with an abandoned stitch.
source share