vendredi 17 avril 2015

How to avoid stackoverflow in clojure recursive function?


Vote count:

0




Here is an example:



;; Helper function for marking multiples of a number as 0
(def mark (fn [[x & xs] k m]
(if (= k m)
(cons 0 (mark xs 1 m))
(cons x (mark xs (inc k) m))
)))

;; Sieve of Eratosthenes
(defn sieve
[x & xs]
(if (= x 0)
(sieve xs)
(cons x (sieve (mark xs 1 x)))
))

(take 10 (lazy-seq (sieve (iterate inc 2))))


It produces a StackOverflowError.



asked 1 min ago

qed

2,972






How to avoid stackoverflow in clojure recursive function?

Aucun commentaire:

Enregistrer un commentaire