Aha... I understand now. Thanks a lot for help. I will try to find other solution. And yes, I'm not looking for "too much" help.
On Jan 26, 3:33 pm, Chouser <chou...@gmail.com> wrote: > On Tue, Jan 26, 2010 at 9: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? > > The problem is that 'filter' is lazy, and each time through the > loop you wrap another layer of filter around numbers. So each > time, (first numbers) is calling through one more layer of filter > than the previous time. Eventually this gets deep enough to > overflow the stack. > > Of course with this algorithm you *need* filter to be lazy, or > you'd never get past the first iteration of the loop. I'm afraid > you'll have to come up with a new algorithm. There are a few > options, but I suppose you don't want too many hints yet? > > --Chouserhttp://joyofclojure.com/ -- 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