Hi, 2010/8/10 David Sletten <da...@bosatsu.net>
> Carlos, > > I think this is pretty much what you had in mind: > (defn count-zeros [l] > (cond (empty? l) 0 > (zero? (first l)) (inc (count-zeros (rest l))) > :else (count-zeros (rest l)))) > > (count-zeros '(9 8 6 0 1 2 0 5)) => 2 > (count-zeros '(9 8 6)) => 0 > (count-zeros '()) => 0 > > Of course the above version is not tail-recursive. However, once you're > written a straightforward recursive function it is often easy to see how to > rewrite it (using an accumulator here) to take advantage of Clojure's recur: > (declare count-zeros-aux) > > (defn count-zeros [l] > (count-zeros-aux l 0)) > > (defn- count-zeros-aux [l result] > (cond (empty? l) result > (zero? (first l)) (recur (rest l) (inc result)) > :else (recur (rest l) result))) > > Here the base function presents the same interface to the user, and the > private auxiliary function takes an additional accumulator argument. > > You could accomplish pretty much the same thing by defining two versions > with different arities: > (defn count-zeros > ([l] (count-zeros l 0)) > ([l result] > (cond (empty? l) result > (zero? (first l)) (recur (rest l) (inc result)) > :else (recur (rest l) result)))) > Though here, the version with different arities "exposes as API for the user" the 2-arity version, but it may not make sense for the user of your function to know about this 2-arity version. I personally prefer the first approach (with a private helper function via defn-) or the last approach (with an embedded loop). Cheers, -- Laurent > > Or you could simplify things by using loop: > (defn count-zeros [l] > (loop [num-list l > result 0] > (cond (empty? num-list) result > (zero? (first num-list)) (recur (rest num-list) (inc result)) > :else (recur (rest num-list) result)))) > > Have all good days, > David Sletten > > On Aug 9, 2010, at 8:24 PM, Carlos Torres wrote: > > Hi to everyone, > > I'm trying to create a function that takes a simple list and returns the > number of zeros in the list. > So I'm assuming that they will enter a list containing only numbers. > This is what I have so far, but it only works when the list empty. Can > somebody tell me what I'm missing? > > (defn count-zeros > "Returns the numbers of zero in a simple sequence of numbers" > [list1] > (cond > (empty? list1) 0 > (not (zero? (first list1))) 0 > :else > (recur (+ 1 (count-zeros (rest list1)))))) > > --Carlos > > -- > 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<clojure%2bunsubscr...@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