There are a dozen of confusion when I use lazySeq.
Question:
(def fib
(lazy-seq
(concat [0 1] (map + fib (rest fib))))) ;; It's Ok
(take 10 fib) ;; Bomb
Got the error message: StackOverflowError clojure.lang.RT.more (RT.java:589)
And the following solutions works well:
(def fib
(concat [0 1] (lazy-seq (map + fib (rest fib))))) ;; Works well
(def fib
(lazy-cat [0 1] (map + fib (rest fib)))) ;; Works well
Both concat
and map
return lazy sequence, why the above programs look like each other but distinguish?
More detailedly, Why the first example (lazy-seq
wrapping the concat
) fail but its following example (lazy-seq
wrapping the map
) sucess?