Re: How to Sum a Sequence?

2011-03-21 Thread Mike Meyer
On Sun, 20 Mar 2011 08:47:30 -0700 (PDT) Christian wrote: > Hello Tassilo! > > I've tested your code and looked at the Clojure Documentation for > 'for'. Given that, I have written > > (reduce +(filter even? (for [fib (fib-seq) :while (< fib 400)] > fib))) > > This gives me the error 'cloj

Re: How to Sum a Sequence?

2011-03-20 Thread Andreas Kostler
>> Do you have any resources or books that help with such things? (Taking >> a problem and solving it the way you did) > > I think, my suggestions are not specific to clojure, but they apply to > any functional language. All of them have functions for filtering > sequences, applying a function t

Re: How to Sum a Sequence?

2011-03-20 Thread Tassilo Horn
Christian writes: Hi Christian, > I would like to thank you for this suggestion and the way you > translated the problem statement into code! Thanks for the compliment. :-) > Do you have any resources or books that help with such things? (Taking > a problem and solving it the way you did) I t

Re: How to Sum a Sequence?

2011-03-20 Thread Michael Wood
Hi On 20 March 2011 17:47, Christian wrote: > Hello Tassilo! > > I've tested your code and looked at the Clojure Documentation for > 'for'. Given that, I have written > > (reduce +(filter even? (for [fib (fib-seq) :while (< fib 400)] > fib))) Or using Daniel's suggestion: (reduce + (filter

Re: How to Sum a Sequence?

2011-03-20 Thread Christian
Hello Tassilo! I've tested your code and looked at the Clojure Documentation for 'for'. Given that, I have written (reduce +(filter even? (for [fib (fib-seq) :while (< fib 400)] fib))) This gives me the error 'clojure.lang.LazySeq cannot be cast to clojure.lang.IFn'. I think this is because

Re: How to Sum a Sequence?

2011-03-20 Thread Daniel Werner
On Mar 20, 10:22 am, Christian wrote: > valued fibonacci terms under four million. (I did not know how to > specify a predicate depending on the value of (fib-seq) to check if > the value is under 4 million.) For this there is #'take-while: (take-while #(< % 400) ...) Daniel -- You receiv

Re: How to Sum a Sequence?

2011-03-20 Thread Andreas Kostler
On 20/03/2011, at 8:25 PM, Christian wrote: > Hello Andreas! > > Thanks for your swift reply. I'll try to explain the reasoning behind > my code. > > In the Clojure Reference I found the function into, which is described > like so: > > Usage: (into to from) > "Returns a new coll consisting of

Re: How to Sum a Sequence?

2011-03-20 Thread Christian
Hello Andreas! Thanks for your swift reply. I'll try to explain the reasoning behind my code. In the Clojure Reference I found the function into, which is described like so: Usage: (into to from) "Returns a new coll consisting of to-coll with all of the items of from-coll conjoined." I'm curren

Re: How to Sum a Sequence?

2011-03-20 Thread Tassilo Horn
Christian writes: Hi Christian, > For those unfamiliar, Project Euler Problem 2 states: > find the sum of all Sounds like (reduce + ...). > even-valued fibonacci terms Sounds like (filter even? ...) > that are less than four million. Hm, that's a bit more challenging. I think, you could go

Re: How to Sum a Sequence?

2011-03-20 Thread Andreas Kostler
Hi Christian, What you're trying to do is to build up a vector with the last element of the vector being the answer to your problem: (last answer) => 4613732 You're trying to use cons (conj) to build up that list. Now, your function below never terminates because you're: a) Not actually buildin

How to Sum a Sequence?

2011-03-20 Thread Christian
I've tried Project Euler 2 now. For those unfamiliar, Project Euler Problem 2 states: find the sum of all even-valued fibonacci terms that are less than four million. Let's assume that I have the code to fill the vector: (def fib-seq ((fn rfib [a b] (lazy-seq (cons a (rfib b (+ a b)