Re: Please help! Really simple function not working.

2010-08-10 Thread David Sletten
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

Re: Please help! Really simple function not working.

2010-08-10 Thread Laurent PETIT
2010/8/10 David Sletten da...@bosatsu.net On Aug 10, 2010, at 2:22 AM, Laurent PETIT wrote: 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

Re: Please help! Really simple function not working.

2010-08-10 Thread Meikel Brandmeyer
Hi, On Aug 10, 8:22 am, Laurent PETIT laurent.pe...@gmail.com wrote: 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

Re: Please help! Really simple function not working.

2010-08-10 Thread Laurent PETIT
2010/8/10 Meikel Brandmeyer m...@kotka.de Hi, On Aug 10, 8:22 am, Laurent PETIT laurent.pe...@gmail.com wrote: 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

Re: Please help! Really simple function not working.

2010-08-10 Thread Meikel Brandmeyer
Hi, On Aug 10, 9:36 am, Laurent PETIT laurent.pe...@gmail.com wrote: Interesting ! Though it seems like a repetition in this case ... Indeed. However, eg. with multimethods this a nice-to-know to supply some meaningful argument info. Sincerely Meikel -- You received this message because you

Re: Please help! Really simple function not working.

2010-08-10 Thread Laurent PETIT
2010/8/10 Meikel Brandmeyer m...@kotka.de Hi, On Aug 10, 9:36 am, Laurent PETIT laurent.pe...@gmail.com wrote: Interesting ! Though it seems like a repetition in this case ... Indeed. However, eg. with multimethods this a nice-to-know to supply some meaningful argument info. Indeed !

Re: Please help! Really simple function not working.

2010-08-10 Thread Laurent PETIT
2010/8/10 Laurent PETIT laurent.pe...@gmail.com 2010/8/10 Meikel Brandmeyer m...@kotka.de Hi, On Aug 10, 9:36 am, Laurent PETIT laurent.pe...@gmail.com wrote: Interesting ! Though it seems like a repetition in this case ... Indeed. However, eg. with multimethods this a nice-to-know to

Re: Please help! Really simple function not working.

2010-08-10 Thread Nicolas Oury
On Tue, Aug 10, 2010 at 2:40 AM, Mark Engelberg mark.engelb...@gmail.com wrote: arbitrary-sized lists is a primary goal.  The posted code (minus the call to recur), is an elegant recursive formulation that is idiomatic in Scheme because Scheme is (typically) implemented in a way that makes

Re: Please help! Really simple function not working.

2010-08-10 Thread Mark Engelberg
On Tue, Aug 10, 2010 at 1:15 AM, Nicolas Oury nicolas.o...@gmail.com wrote:  So, in this particular case, Scheme does not warranty no exhaustion of resources. Yes, but your recursion depth is limited to the length of the list you are processing. So if you have enough resources to comfortably

Re: Please help! Really simple function not working.

2010-08-10 Thread Laurent PETIT
Hello Mark, 2010/8/10 Mark Engelberg mark.engelb...@gmail.com On Tue, Aug 10, 2010 at 1:15 AM, Nicolas Oury nicolas.o...@gmail.com wrote: So, in this particular case, Scheme does not warranty no exhaustion of resources. Yes, but your recursion depth is limited to the length of the list

Re: Please help! Really simple function not working.

2010-08-10 Thread Nicolas Oury
On Tue, Aug 10, 2010 at 9:55 AM, Mark Engelberg mark.engelb...@gmail.com wrote:  In Clojure, I find stack limitations are a real issue unless I transform the algorithms into a tail-recursive accumulator style. For lists, it's usually not hard.  For trees, it can be a bit of a pain. Try

Re: Please help! Really simple function not working.

2010-08-10 Thread Nicolas Oury
On Tue, Aug 10, 2010 at 10:08 AM, Laurent PETIT laurent.pe...@gmail.com wrote: Naive question from someone who has not really used Scheme in practice : beyond the memory footprint problem (which may or may not be a problem depending on the memory size of an element in the initial list, and also

Re: Please help! Really simple function not working.

2010-08-10 Thread Laurent PETIT
2010/8/10 Nicolas Oury nicolas.o...@gmail.com On Tue, Aug 10, 2010 at 10:08 AM, Laurent PETIT laurent.pe...@gmail.com wrote: Naive question from someone who has not really used Scheme in practice : beyond the memory footprint problem (which may or may not be a problem depending on the

Re: Please help! Really simple function not working.

2010-08-10 Thread Laurent PETIT
2010/8/10 Laurent PETIT laurent.pe...@gmail.com 2010/8/10 Nicolas Oury nicolas.o...@gmail.com On Tue, Aug 10, 2010 at 10:08 AM, Laurent PETIT laurent.pe...@gmail.com wrote: Naive question from someone who has not really used Scheme in practice : beyond the memory footprint problem

Re: Please help! Really simple function not working.

2010-08-10 Thread Mark Engelberg
On Tue, Aug 10, 2010 at 2:21 AM, Nicolas Oury nicolas.o...@gmail.com wrote: It would probably be up to twice as slow, I would say. For a list that is continuous in memory and continuations that are allocated perfectly in memory, you would need to go through twice the same amount of memory. (I

Re: Please help! Really simple function not working.

2010-08-10 Thread Meikel Brandmeyer
Hi, On Aug 10, 11:34 am, Mark Engelberg mark.engelb...@gmail.com wrote: The table shows that the performance of the accumulator-style version of factorial is always worse than that of the original factorial function. I'm a little bit surprised, that people still prefer programs, which are

Re: Please help! Really simple function not working.

2010-08-10 Thread Nicolas Oury
The table show 20!. I am far from being sure of my point, but in a first approximation: Loading a part of memory that is not cached (which will be the case for big lists) is around 300 cycles. An addition in a register is a few cycles, a jump is a few cycles too, at most, (the prediction will be

Re: Please help! Really simple function not working.

2010-08-10 Thread Will M. Farr
Maybe this point about Racket is too off-topic for the list, but: On Aug 10, 2010, at 4:55 AM, Mark Engelberg wrote: Generally speaking, I find I do not have to worry about blowing the stack in Scheme, and as a result, non-tail-call structural recursion (such as the algorithm that kicked off

Re: Please help! Really simple function not working.

2010-08-10 Thread Nicolas Oury
On Tue, Aug 10, 2010 at 11:23 AM, Will M. Farr wmf...@gmail.com wrot Sorry for the digression; I hope people find it interesting. I found it interesting. -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to

Please help! Really simple function not working.

2010-08-09 Thread Carlos Torres
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?

Re: Please help! Really simple function not working.

2010-08-09 Thread Michael Gardner
On Aug 9, 2010, at 7:24 PM, Carlos Torres wrote: I'm trying to create a function that takes a simple list and returns the number of zeros in the list. user= (count (filter zero? [0 1 2 3 0 4 5 6 0 7 8 9])) 3 -- You received this message because you are subscribed to the Google Groups

Re: Please help! Really simple function not working.

2010-08-09 Thread gary ng
On Mon, Aug 9, 2010 at 5:24 PM, Carlos Torres carlos.torr...@upr.edu 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,

Re: Please help! Really simple function not working.

2010-08-09 Thread Laurent PETIT
2010/8/10 Carlos Torres carlos.torr...@upr.edu 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

Re: Please help! Really simple function not working.

2010-08-09 Thread Phil Hagelberg
On Mon, Aug 9, 2010 at 5:24 PM, Carlos Torres carlos.torr...@upr.edu wrote: (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))

Re: Please help! Really simple function not working.

2010-08-09 Thread Mark Engelberg
On Mon, Aug 9, 2010 at 6:00 PM, Phil Hagelberg p...@hagelb.org wrote: Just remove the call to recur and it will work, albeit only for lists small enough to not blow the stack. I'm assuming from the tone of the original post that the author is trying to generally understand how to write

Re: Please help! Really simple function not working.

2010-08-09 Thread Carlos Torres
On Mon, Aug 9, 2010 at 9:00 PM, Phil Hagelberg p...@hagelb.org wrote: On Mon, Aug 9, 2010 at 5:24 PM, Carlos Torres carlos.torr...@upr.edu wrote: (defn count-zeros Returns the numbers of zero in a simple sequence of numbers [list1] (cond (empty? list1) 0 (not (zero?