Hello,
At some point I needed at "flatten" function taking a list of atomic
elements or nested lists, and producting a "flat" list of only atomic
elements (in the same order). It should be lazy.

This is what I came up with. Can anyone see a more elegant solution (I
feel I am working "low-level" somehow).

user> (defn flatten [lst]
        (lazy-seq
          (if (empty? lst) lst
              (let [[x & xs] lst]
                (if (list? x)
                  (concat (flatten x) (flatten xs))
                  (cons x (flatten xs)))))))
#'user/flatten
user> (flatten '(1 2 3 4))
(1 2 3 4)
user> (flatten '((1 2) (3) 4))
(1 2 3 4)
user> (flatten '(((1) 2) (3) 4))
(1 2 3 4)
user>

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