For my prime numbers lazy seq, I am checking to see if an index value is divisible by all the primes below that current index (prime?
). The problem is, when I call primes within itself (primes
within shr-primes
line), it only returns the initial value. Is it possible to keep the lazy-seq updated while building it lazily? It seems counter-intuitive to the lazy-seq concept.
(def primes
(cons 2 (for [x (range)
:let [y (-> x (* 2) (+ 3))
root (math/floor (math/sqrt y))
shr-primes (take-while (partial >= root) primes) ;; primes stuck at init value
prime? (every? #(not= % 0) (pmap #(rem y %) shr-primes))]
:when prime?]
y)))
y
in the last line withprimes
, then you'll see thatprimes
is stuck at the initial value and not updated as it should be. – Paul Lam