I was about to ask the same question on this list about this code
adapted from SICP section 3.5.3

<pre>
(defn pi-summands [n]
  (cons (/ 1.0 n)
        (lazy-seq (map - (pi-summands (+ n 2))))))
</pre>

which causes a stack overflow when I run

(nth (pi-summands 1) 10000)

but it looks like you have answered this question. To get the 10,000th
element from this list means that we have to keep 9999 map calls on
the stack with all of their context. Is that correct?

Is it also true that the Scheme version of this code would have the
same problem?

<pre>
(define (pi-summands n)
  (cons-stream (/ 1.0 n)
        (stream-map - (pi-summands (+ n 2)))))
</pre>

I would hope so. I don't want to get Scheme envy.

Thanks for your help,
Brenton

On Jan 26, 6:17 am, Nebojsa Stricevic <nebojsa.strice...@gmail.com>
wrote:
> Greetings,
>
> I'm new to Clojure and working my way through Project Euler problems
> for learning. I wrote function for calculating prime number below some
> integer value max. But it doesn't work for large numbers, causing
> StackOverflowError.
>
> (defn primes-below
>         "calculates all primes bellow max"
>         [max]
>         (loop [numbers (range 2 max) primes []]
>                 (if (empty? numbers)
>                         primes
>                         (let [p (first numbers)
>                               numbers (filter #(pos? (rem % p)) numbers)
>                               primes (cons p primes)]
>                           (recur numbers primes)))))
>
> (primes-below 2000000)
>
> I guess problem is not with loop-recur, or am I wrong?
>
> Thanks in advance.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to