<br><br><div><span class="gmail_quote">On 2/10/07, <b class="gmail_sendername">Lennart Augustsson</b> <<a href="mailto:lennart@augustsson.net">lennart@augustsson.net</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
There are many things that makes your code slow.<br>* The default for Haskell is to compute with Integer, not Int. So<br>that makes from Integral and floor very slow.<br>* foldl' is a bad choice, because it is too strict, you want to abort
<br>the loop as soon as possible.</blockquote><div><br>Now why is foldl' too strict? I don't think I understand? <br></div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
* your take is really wrong. The number of primes you need to take<br>cannot be computed like that. You want to take primes while the sqrt<br>of x is larger than the prime.</blockquote><div><br>Yeah, I don't know what the %#*( happened there. I should have proofread.
<br></div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Also, your code is not idiomatic Haskell.<br><br>Here's a different version:
<br><br>primes :: [Int]<br>primes = 2:filter isPrime [3,5..]<br> where isPrime x = all (\ y -> x `mod` y /= 0) $ takeWhile (\ p -<br> > p*p <= x) primes<br><br><br>On Feb 10, 2007, at 21:02 , Creighton Hogg wrote:
<br><br>> primes = 2:(foldr (\x y -> if isPrime x then x:y else y) [] [3..])<br>> where isPrime x = foldl' (\z y -> z && (if x `mod` y == 0 then<br>> False else True)) True (take (floor $ sqrt $ fromIntegral x) primes)
<br><br></blockquote></div><br>