Chain call in clojure?

Posted by Konrad Garus on Stack Overflow See other posts from Stack Overflow or by Konrad Garus
Published on 2010-06-05T13:24:19Z Indexed on 2010/06/05 13:52 UTC
Read the original article Hit count: 315

Filed under:
|
|

I'm trying to implement sieve of Eratosthenes in Clojure. One approach I would like to test is this:

  1. Get range (2 3 4 5 6 ... N)
  2. For 2 <= i <= N
    • Pass my range through filter that removes multiplies of i
    • For i+1th iteration, use result of the previous filtering

I know I could do it with loop/recur, but this is causing stack overflow errors (for some reason tail call optimization is not applied).

How can I do it iteratively? I mean invoking N calls to the same routine, passing result of ith iteration to i+1th.

© Stack Overflow or respective owner

Related posts about algorithm

Related posts about clojure