no, we have not moved past this, and the error is absolutely because you 
take too much for granted about lazy behavior

(defn divides? [x y] (zero? (mod x y)))


(defn prime-ub [x] (/ x (if (even? x) 2 3)))


(defn lower-primes [primes x] (let [ub (prime-ub x)]
(println "primes" primes "x" x)
(take-while #(<= % ub) primes)))


(defn prime? [primes x] (not-any? #(divides? x %)(lower-primes primes x)))


user=> (def primes (cons 2 (lazy-seq (filter #(prime? primes %) (drop 3 
(range))))))
#'user/primes
user=> (take 20 primes)
(primes (2) x 3
primes (2) x 4
primes (2) x 5
primes (2) x 6
primes (2) x 7
primes (2) x 8
primes (2) x 9
primes (2) x 10
primes (2) x 11
primes (2) x 12
primes (2) x 13
primes (2) x 14
primes (2) x 15
primes (2) x 16
primes (2) x 17
primes (2) x 18
primes (2) x 19
primes (2) x 20
primes (2) x 21
primes (2) x 22
primes (2) x 23
primes (2) x 24
primes (2) x 25
primes (2) x 26
primes (2) x 27
primes (2) x 28
primes (2) x 29
primes (2) x 30
primes (2) x 31
2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 primes (2 3 5 7 9 11 13 15 17 19 21 
23 25 27 29 31) x 32
primes (2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31) x 33
primes (2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31) x 34
primes (2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31) x 35
primes (2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31) x 36
primes (2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31) x 37
primes (2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31) x 38
primes (2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31) x 39
primes (2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31) x 40
primes (2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31) x 41
primes (2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31) x 42
primes (2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31) x 43
primes (2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31) x 44
primes (2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31) x 45
primes (2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31) x 46
primes (2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31) x 47
primes (2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31) x 48
primes (2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31) x 49
primes (2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31) x 50
primes (2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31) x 51
primes (2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31) x 52
primes (2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31) x 53
primes (2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31) x 54
primes (2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31) x 55
primes (2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31) x 56
primes (2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31) x 57
primes (2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31) x 58
primes (2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31) x 59
primes (2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31) x 60
primes (2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31) x 61
primes (2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31) x 62
primes (2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31) x 63
31 37 41 43 47)


On Thursday, February 12, 2015 at 6:47:34 PM UTC-8, Jorge Marques Pelizzoni 
wrote:
>
>
> So why isn't 4 and any even numbers in the result list? Empty primes' 
> allows everything to pass. We are already beyond this. I've already posted 
> that even this does not work:
>
> (def primes (cons 2 (lazy-seq (filter #(prime? primes %) (drop 3 
> (range))))))
>
>
> Em sexta-feira, 13 de fevereiro de 2015 00:39:03 UTC-2, Justin Smith 
> escreveu:
>>
>> Considering for the sake of argument the possibility that it is a 
>> legitimate bug, and not a result of misusing the language features, it is a 
>> family of bug that will be more common than most, because it reflects a 
>> style of programming that is rare in real Clojure code.
>>
>> But it isn't a bug.
>>
>> Lazy-seqs don't promise to be "as lazy as possible", and when we step 
>> through the logic of the original code in Michael Blume's post, we can see 
>> why chunking breaks it. Taking for granted that something lazy is realized 
>> with a specific granularity is an error in Clojure (see also lazy-seqs used 
>> inside with-open blocks or db transactions - the opposite error is common, 
>> not being strict enough).
>>
>> defn primes [] (let [primes' (atom nil)]
>>                  (reset! primes' (cons 2 (filter #(prime? @primes' %) 
>> (drop 3 (range)))))))
>>
>> The first time through the above code, primes' will be empty, so the 
>> first chunk of results will all be calculated with an empty list of 
>> candidate divisors.
>>
>>>

-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to