After thinking about it some more, I don't understand it either.  I have 
created a ticket for this: 
https://www.assembla.com/spaces/clojure/tickets/457-lazy-recursive-definition-giving-incorrect-results.

As a workaround, eliminate the lazy intermediary "nums" sequence, e.g. 

(def primes
(concat
 [2]
 (lazy-seq
  (let [primes-from
        (fn primes-from
          [n]
          (if (some #(zero? (rem n %))
                    (take-while #(<= (* % %) n) primes))
            (recur (+ n 2))
            (lazy-seq (cons n (primes-from (+ n 2))))))]
    (primes-from 3)))))

Stu

> Thank you all for explaining this to me but I still don't understand
> clojures behavior in this case,
> 
> Try running this code:
> 
> (def nums (drop 2 (range)))
> (def primes (cons (first nums)
>              (lazy-seq (->>
>                (rest nums)
>                (remove
>                  (fn [x]
>                    (let [dividors (take-while #(<= (* % %) x)
> primes)]
>                      (println (str "primes = " primes))
>                      (some #(= 0 (rem x %)) dividors))))))))
> (take 5 primes)
> 
> It prints out:
> (primes = (2)
> primes = (2)
> primes = (2)
> primes = (2)
> primes = (2)
> primes = (2)
> primes = (2)
> primes = (2)
> primes = (2)
> primes = (2)
> primes = (2)
> primes = (2)
> primes = (2)
> primes = (2)
> primes = (2)
> primes = (2)
> primes = (2)
> primes = (2)
> primes = (2)
> primes = (2)
> primes = (2)
> primes = (2)
> primes = (2)
> primes = (2)
> primes = (2)
> primes = (2)
> primes = (2)
> primes = (2)
> primes = (2)
> 2 3 5 7 9)
> 
> How can (println primes) ever print out (2) ? That means that primes
> must have length 1 and that should be impossible no matter how chunked
> it is. It's hard to believe that that is intended behavior.
> I don't insist that a recursive lazy definition needs to work. It
> could also throw an error. But simply giving incorrect and
> unpredictable results can't be a good solution in my opinion.
> 
> -- 
> 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 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

Reply via email to