First of all, you should switch from ((fn [ls wip ans] ...) ls [] nil)
to (loop [ls ls wip [] ans nil] ...) Read about it here: http://clojure.org/special_forms Using higher-order functions, you could do: (defn split-zero [coll] (if (seq coll) (let [divided (partition-by zero? coll)] (take-nth 2 (if (zero? (ffirst divided)) (rest divided) divided))))) Step by step: (partition-by zero? coll) '(1 2 3 0 4 5 6 0 7 8 9) -> '((1 2 3) (0) (4 5 6) (0) (7 8 9)) (if (zero? (ffirst divided)) (rest divided) divided) If the first partition consists of zeroes, skip it. (take-nth 2 ...) '(3 1 7 3 0) -> '(3 7 0) On Thu, Sep 29, 2011 at 1:35 PM, Peter Hull <peterhul...@gmail.com> wrote: > Hi All, > I am just learning clojure and I've written a function to split a list (see > docstring for details). I was wondering if any of you experienced hands > could take a look at it and comment. I've never used lisp or a functional > language before so I was wondering if I was doing it right or if there is a > better way. Anyway here is the function: > > (defn split-zero > "Split a collection at its zero values into a list of lists. > Multiple zeros are treated as one. > The order of elements in each inner list is maintained but > the order of inner lists in the result may be changed. > e.g. (split-zero '(1 2 3 0 4 5 6 0 7 8 9)) > -> ((7 8 9) (4 5 6) (1 2 3))" > [ls] ((fn [ls wip ans] > (if (empty? ls) > (if (empty? wip) ans (conj ans (list* wip))) > (if (zero? (first ls)) > (if (empty? wip) > (recur (next ls) [] ans) > (recur (next ls) [] (conj ans (list* wip)))) > (recur (next ls) (conj wip (first ls)) ans)))) ls [] nil)) > > Thanks, > Peter > > -- > 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 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