Hi maclo, Your code assume there is two "primes" sequences: the result and the one being constructed. But there's only one and which is infinite. You define your sequence as being (cons 2 something) and computing "something" requires evaluating (every? #(pos? (mod 3 %)) (cons 2 something)) since (pos? (mod 3 2)) is true, every? continues processing the seq and tries to evaluate (every? #(pos? (mod 3 %)) something). So computing "something" requires knowing "something"!
The take-while is useful in that it reduces the scope of every to primes already computed. Your code works only because of a bug/unspecified behaviour which makes the recursive reference of the sequence to be considered nil. hth, Christophe On Sun, Dec 2, 2012 at 4:30 PM, maclo <maclo7ma...@gmail.com> wrote: > Hi > > > user=> (def primes > >> (cons 2 >> (filter >> (fn isprime[n] >> >> (every? >> #(pos? (mod n %)) >> (take-while #(<=(* % %)n) primes))) >> (iterate inc 3)))) >> user=> (take 50 primes) >> (2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 >> 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 >> 197 199 211 223 227 229) >> > > Is it really necessary to call 'take-while' ? The version without it > > > (def primes > (cons 2 > (filter > (fn isprime? [n] > (every? #(pos? (mod n %)) primes)) > (iterate inc 3)))) > > works for me as well. Is it safe to use this version or is using > 'take-while' for some reason necessary ? > > maclo > -- On Clojure http://clj-me.cgrand.net/ Clojure Programming http://clojurebook.com Training, Consulting & Contracting http://lambdanext.eu/ -- 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