Take the 2-minute tour ×

Almost 2 identical programs to generate infinite lazy seqs of randoms. The first doesn't crash. The second crash with OutOfMemoryError exception. Why?

;Return infinite lazy sequence of random numbers    
(defn inf-rand[] (lazy-seq (cons (rand) (inf-rand))))    

;Never returns. Burns the CPU but won't crash and lives forever.    
(last (inf-rand))

But the following crash pretty quickly:

;Return infinite lazy sequence of random numbers    
(def r1 (inf-rand))

;Crash with "OutOfMemoryError"
 (last r1)
share|improve this question
add comment

1 Answer

up vote 24 down vote accepted

I believe this is an example of "holding onto the head".

By making the reference r1 in the second example you open up the possibility of later saying something like (first r1) so you will end up storing the members of your lazy-seq as they are reified.

In the first case Clojure can determine that nothing will ever be done with earlier members of the infinite sequence so they can be disposed of and not consume memory.

I am still very much a Clojure beginner myself, any comments or corrections to my understanding or terminology greatly appreciated.

share|improve this answer
I'm a beginner too, but your explanation looks very correct. I would have answered the same thing if you had not beaten me to it! And 6 upvoters seem to agree with you. –  Carl Smotricz Nov 18 '09 at 19:56
When I started doing Project Euler problems in Clojure a while back, my debug prints on infinite lazy sequences slowed my programs down... infinitely. Infinite lazy sequences are an important Clojure concept to come to grips with. –  Carl Smotricz Nov 18 '09 at 19:58
BTW, how come there is no StackOverflow. There is an infinite recursion in inf-rand –  GabiMe Nov 19 '09 at 9:30
There is no stack overflow because the lazy evaluation does not recurse no the stack. Each element is evaluated and returns. –  Eric Normand Nov 24 '09 at 20:49
add comment

Your Answer


