Learning clojure, trying to create a lazy infinite sequence of all primes. I know that there are more efficient algorithms; I do the following more as a POC / lesson than as an ideal solution.
I have a function that, given a sequence of primes, tells me the following:
(next-prime [2 3 5]) ; result: 7
Therefore, my lazy sequence should go to this function, then take the result and add it to myself.
My first attempt:
(def lazy-primes (lazy-cat [2 3 5] (find-next-prime lazy-primes)))
.., which leads to an IllegalArgumentException: I donβt know how to create ISeq from: java.lang.Integer
My second attempt:
(def lazy-primes (lazy-cat [2 3 5] [(find-next-prime lazy-primes)]))
.. which gives me [2 3 5 7] when asked for 10 items.
Attempt 3:
(def lazy-primes (lazy-cat [2 3 5] (conj lazy-primes (find-next-prime lazy-primes)))) (take 10 lazy-primes) ; result: (2 3 5 7 2 3 5 7 2 3)
They all look as if they should work (or at least they should work, given that the previous ones didn't work). Why do I get fictitious output for each case?
source share