This is a pretty common question. http://stackoverflow.com/questions/2946764/recursive-function-causing-a-stack-overflow is one instance that's been answered fairly thoroughly; does it help you?
On May 18, 3:10 am, Szymon Ząbkiewicz <[email protected]> wrote: > Hello, > I'm trying to learn Clojure so I'm making some simple programs. One of > them is prime sieve which looks like this: > > (defn filter-primes [nums] > "Leave only numbers not divisable by the first element in the list" > (filter (fn [x] (not= 0 (mod x (first nums)))) (rest nums))) > > (defn sieve [max-num] > "Calculate all primes between 1 and max-num inclusive" > (loop [nums (range 2 (inc max-num)) > primes (list)] > (if (empty? nums) > (reverse primes) > (recur (filter-primes nums) (conj primes (first nums)))))) > > The problem is that calling for example (sieve 10000) already twrows > StackOverflowError, which is kind of suprising to me, because I use > the loop...recure. I'm not looking for an anwser with a totally > different approach, but how to make this recursive approach work. -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to [email protected] Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/clojure?hl=en
