It's worth understanding how to go back and forth between accumulator-style
and a lazy construction.  You can convert the above non-lazy accumulator
version into a similar version that is lazy but has no risk of stack
overflow in the realization of that lazy flattened list:

(defn my-flatten
  [xs]
  (if (empty? xs) ()
    (let [x (first xs), ys (rest xs)]
      (if (sequential? x)
        (if (seq x)
          (recur (cons (first x) (cons (rest x) ys)))
          (recur ys))
        (lazy-seq (cons x (my-flatten ys)))))))

Basically, you just get rid of the accumulator, and in the place where you
would have conj'd in the next atomic element, you just build the lazy
sequence.

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to