Hi,

for one of the problems from Project Euler I implemented a "triangle
numbers" lazy sequence after the blueprint of the fibonacci numbers
sequence in clojure.contrib.lazy-seqs:. When taking large numbers of
items from this  equence, I get an out-of-memory exception:

user=> (def triangle-numbers
    (let [rest-fn
        (fn rest-fn [current cumulated]
            (let [next (+ current cumulated)]
              (lazy-cons next (rest-fn (inc current) next))))]
        (rest-fn 1 0)))
#'user/triangle-numbers
user=> (nth triangle-numbers 1000000)
java.lang.OutOfMemoryError: Java heap space
java.lang.OutOfMemoryError: Java heap space
        at java.math.BigInteger.add(BigInteger.java:1068)
        at java.math.BigInteger.add(BigInteger.java:1041)
        at clojure.lang.Numbers$BigIntegerOps.add(Numbers.java:793)
        at clojure.lang.Numbers.add(Numbers.java:127)
        at user.fn__2290$rest_fn__2292.invoke(Unknown Source)
        at user.fn__2290$rest_fn__2292$fn__2294.invoke(Unknown Source)
        at clojure.lang.LazyCons.rest(LazyCons.java:60)
        at clojure.lang.RT.nth(RT.java:702)
        at clojure.nth__440.invoke(boot.clj:830)
        at user.eval__2303.invoke(Unknown Source)
        at clojure.lang.Compiler.eval(Compiler.java:3891)
        at clojure.lang.Repl.main(Repl.java:75)

If I understand it correctly this is the effect of the caching of the
lazy sequence. Is there a way to suppress the  caching? Are there
alternatives in Clojure for representing and processing very large
sequences?

Thanks
Stephan

P.S.: I read the recent thread "memory issue with nth", but it is not
clear to me whether this is the same problem.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to