On Sep 3, 1:13 am, Sudish Joseph <sud...@gmail.com> wrote:

> (defn flatten-2 [lst]
>   (lazy-seq
>     (if-let [x (first lst)]
>       (let [xs (rest lst)]
>         (if (seq? x)
>           (concat (flatten-2 x) (flatten-2 xs))
>           (cons x (flatten-2 xs)))))))

This version is broken:
user=> (flatten-2 '(:a (:b :c) false :d :e))
(:a :b :c)

Never check with first for nil. Always check explicitely with seq!

(defn flatten-3
  [s]
  (lazy-seq
    (when-let [s (seq s)]
      (let [fst (first s)]
        (if (seq? fst)
          (concat (flatten-3 fst) (flatten-3 (rest s)))
          (cons fst (flatten-3 (rest s))))))))

user=> (flatten-3 '(:a (:b :c) false :d :e))
(:a :b :c false :d :e)

Sincerely
Meikel

--~--~---------~--~----~------------~-------~--~----~
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