Re: Why I'm getting StackoverflowError?

2010-12-07 Thread Meikel Brandmeyer
Hi, Am 06.12.2010 um 22:38 schrieb Ken Wesson: A reduce that discards one of its arguments (e.g. (reduce (fn [n _] (inc n)) coll)) also strikes me as slightly icky. :) Indeed this would fit in the 5% left. Anyway: I can't remember writing a function with accumulator not using the other

Re: Why I'm getting StackoverflowError?

2010-12-06 Thread nickik
What is the difference between rest and next? This has to do with lazyness. I wrote an answer to that on stackoverflow. http://stackoverflow.com/questions/4288476/clojure-rest-vs-next Should answer everthing. I'm confused, should I use empty? or not? when to use it? Why Clojure decided to

Re: Why I'm getting StackoverflowError?

2010-12-06 Thread Ken Wesson
On Mon, Dec 6, 2010 at 4:02 AM, nickik nick...@gmail.com wrote: The easy (and good) solution is to pass the running result onlong the way. This can be done in diffrent ways. First the CL Style where you creat a new function in you function that then does all the work. (from Ken Wesson)

Re: Why I'm getting StackoverflowError?

2010-12-06 Thread HB
I didn't expect my question would initiate such a wonderful discussion, I'm speechless. Thank you all guys, you are amazing. Alex, your posts killed in a very good way :) It was really helpful to morph the code and transform it. On Dec 6, 11:14 am, Ken Wesson kwess...@gmail.com wrote: On Mon,

Re: Why I'm getting StackoverflowError?

2010-12-06 Thread Meikel Brandmeyer
Hi, Am 06.12.2010 um 10:14 schrieb Ken Wesson: Then we could use arity functions. (Alex Osborne) (defn list-length ([coll] (list-length coll 0)) ([coll n] (if-let [s (seq coll)] (recur (rest s) (inc n)) n))) This is nice style in

Re: Why I'm getting StackoverflowError?

2010-12-06 Thread Ken Wesson
On Mon, Dec 6, 2010 at 5:19 AM, Meikel Brandmeyer m...@kotka.de wrote: Hi, Am 06.12.2010 um 10:14 schrieb Ken Wesson: Then we could use arity functions. (Alex Osborne)    (defn list-length      ([coll]   (list-length coll 0))      ([coll n] (if-let [s (seq coll)]                    

Re: Why I'm getting StackoverflowError?

2010-12-06 Thread nickik
On Dec 6, 11:40 am, Ken Wesson kwess...@gmail.com wrote: Won't that make the internal recursive call fail though? And even if not -- ugly IMO. :) Agree. Why make something slower and more ugly? -- You received this message because you are subscribed to the Google Groups Clojure group. To

Re: Why I'm getting StackoverflowError?

2010-12-06 Thread Meikel Brandmeyer
Hi, Am 06.12.2010 um 11:40 schrieb Ken Wesson: Won't that make the internal recursive call fail though? No. Because the metadata is just documentation. And even if not -- ugly IMO. :) Tastes vary. I prefer this over a second toplevel function named foo-aux or the like. I also prefer it

Re: Why I'm getting StackoverflowError?

2010-12-05 Thread Danny Woods
On Dec 5, 9:52 pm, HB hubaghd...@gmail.com wrote: Hi, I'm trying to write a function that calculates the length of a list: (defn list-length [col]   (if col     (+ 1 (list-length(rest col)))     0)) (list-length '(Java, Clojure, Scala)) Upon running it in the REPL, I got the error:

Re: Why I'm getting StackoverflowError?

2010-12-05 Thread Robert McIntyre
Your function never actually ends because even the empty list evaluates to true :) rlm.dna-melting (if (rest '()) true false) true rlm.dna-melting (if (next '()) true false) false so, changing your list length function to use next will work rlm.dna-melting (defn list-length [col] (if col (+ 1

Re: Why I'm getting StackoverflowError?

2010-12-05 Thread Ken Wesson
On Sun, Dec 5, 2010 at 5:16 PM, Robert McIntyre r...@mit.edu wrote: Your function never actually ends  because even the empty list evaluates to true :) rlm.dna-melting (if (rest '()) true false) true rlm.dna-melting (if (next '()) true false) false so, changing your list length function

Re: Why I'm getting StackoverflowError?

2010-12-05 Thread HB
What is the difference between rest and next? I'm confused, should I use empty? or not? when to use it? Robert, Your code is working but if I use empty? , it returns 0 instead of the actual count. Why? Why Clojure decided to handle an empty list as a not false? this is a big (if not) departure

Re: Why I'm getting StackoverflowError?

2010-12-05 Thread Alex Osborne
HB hubaghd...@gmail.com writes: I'm trying to write a function that calculates the length of a list: (defn list-length [col] (if col (+ 1 (list-length(rest col))) 0)) (list-length '(Java, Clojure, Scala)) Upon running it in the REPL, I got the error:

Re: Why I'm getting StackoverflowError?

2010-12-05 Thread Nadeem Vawda
In Clojure, empty sequences are not considered logically false, so your code continues going after reducing col to an empty list. You need to explicitly check whether the collection is empty, like so: (defn list-length [col] (if (empty? col) 0 (+ 1 (list-length (rest col) Cheers,

Re: Why I'm getting StackoverflowError?

2010-12-05 Thread HB
OMG, this is too much Clojure code for me to handle O.o Alex, you just killed me :) Do you previous Lisp knowledge? or Clojure is your first Lisp? you are so good. I will spend the next couple of hours studying it. Thanks all, you are awesome. On Dec 6, 12:47 am, Alex Osborne a...@meshy.org

Re: Why I'm getting StackoverflowError?

2010-12-05 Thread HB
Ken Alex, Why you aren't calling empty? when you want to check if a collection is empty? Isn't (if s) supposed to return true if s is empty ? On Dec 6, 12:27 am, Ken Wesson kwess...@gmail.com wrote: On Sun, Dec 5, 2010 at 5:16 PM, Robert McIntyre r...@mit.edu wrote: Your function never

Re: Why I'm getting StackoverflowError?

2010-12-05 Thread Ken Wesson
On Sun, Dec 5, 2010 at 6:55 PM, HB hubaghd...@gmail.com wrote: Ken Alex, Why you aren't calling empty? when you want to check if a collection is empty? Isn't (if s) supposed to return true if s is empty ? If coll is empty, (seq coll) and (next coll) are both nil, which is logical false. --

Re: Why I'm getting StackoverflowError?

2010-12-05 Thread Alex Osborne
HB hubaghd...@gmail.com writes: OMG, this is too much Clojure code for me to handle O.o Alex, you just killed me :) Hehe, sorry. Just thought it might be helpful to show the progression of dealing with all the little edge cases. It perhaps looks much more fiddly, but you're doing more there

Re: Why I'm getting StackoverflowError?

2010-12-05 Thread Alex Osborne
HB hubaghd...@gmail.com writes: Ken Alex, Why you aren't calling empty? when you want to check if a collection is empty? Here's the definition of empty? from clojure/core.clj: (defn empty? Returns true if coll has no items - same as (not (seq coll)). Please use the idiom