Re: Filtering lazy sequence on itself - Eratosthenes Sieve

2015-11-05 Thread Sam Raker
Clojure doesn't do TCO with recursive functions--you have to use `loop`/`recur`[1]. This is one of the (thankfully few) "gotchas" in Clojure, at least/especially for people coming from other functional languages (or so I assume--coming from Python, I hadn't heard that much about TCO in the firs

Re: Filtering lazy sequence on itself - Eratosthenes Sieve

2015-11-05 Thread Matthew Ulrich
Thanks, Sergio - this is a good way to construct the sieve. It took me a little while to convince myself that it's the same, but it's definitely a good solution. In general, I guess it's not possible to generate an infinite sequence that utilizes the tail-call optimization it makes sense as

Re: Filtering lazy sequence on itself - Eratosthenes Sieve

2015-11-04 Thread Sergio Rupena
Hi You can try the following (defn dividers [primes n] (take-while #(<= (* % %) n) primes)) (defn prime? [primes n] (every? #(pos? (rem n %)) (dividers primes n))) (defn range-peek [coll] (iterate inc (-> coll peek inc))) (defn sieve ([] (cons 2 (lazy-seq (sieve [2] (