What I still can't get is how does 'lazy-seq' prevent stack overflow. E.g.

(defn my-map [f coll] (when-let [s (seq coll)] (cons (f (first s)) (my-map 
f (rest s))))))
(nth (my-map inc (range 1000000)) 999999)
=> StackOverflowError

(defn my-map [f coll] (lazy-seq (when-let [s (seq coll)] (cons (f (first 
s)) (my-map f (rest s)))))))
(nth (my-map inc (range 1000000)) 999999)
=> 1000000

The second lazy approach still needs to traverse all the sequence 
recursively, why doesn't it cause stack overflow?

-- 
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
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to