What to call the "lazy" made so far during the construction of the lazy secant?

For my lazy seq primes, I check if the index value is divisible by all primes below this current index ( prime?). The problem is that when I call primes inside of me ( primesinside the string shr-primes), it returns only the initial value. Is it possible to update lazy seq while creating lazily? Seems contrary to the concept of lazy seq.

(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)))
+3
source share
1 answer

If you are running Project Euler problems, I don’t want to ruin the exercise for you, but here is how you define the Fibonacci sequence so that lazy-seq saves the “update” on its own:

(defn fib-maker
  ([] (concat [0 1] (fib 0 1)))
  ([a b] (lazy-seq (cons b (fib b (+ a b))))))

(def fib (fib-maker))

, , , , . , , , .

+2

All Articles