<br><br><div><span class="gmail_quote">On 2/10/07, <b class="gmail_sendername">Lennart Augustsson</b> &lt;<a href="mailto:lennart@augustsson.net">lennart@augustsson.net</a>&gt; 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.&nbsp;&nbsp;So<br>that makes from Integral and floor very slow.<br>* foldl&#39; 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&#39; too strict?&nbsp; I don&#39;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.&nbsp;&nbsp;The number of primes you need to take<br>cannot be computed like that.&nbsp;&nbsp;You want to take primes while the sqrt<br>of x is larger than the prime.</blockquote><div><br>Yeah, I don&#39;t know what the %#*( happened there.&nbsp; 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&#39;s a different version:
<br><br>primes :: [Int]<br>primes = 2:filter isPrime [3,5..]<br>&nbsp;&nbsp;&nbsp;&nbsp; where isPrime x = all (\ y -&gt; x `mod` y /= 0) $ takeWhile (\ p -<br> &gt; p*p &lt;= x) primes<br><br><br>On Feb 10, 2007, at 21:02 , Creighton Hogg wrote:
<br><br>&gt; primes = 2:(foldr (\x y -&gt; if isPrime x then x:y else y) [] [3..])<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; where isPrime x = foldl&#39; (\z y -&gt; z &amp;&amp; (if x `mod` y == 0 then<br>&gt; False else True)) True (take (floor $ sqrt $ fromIntegral x) primes)
<br><br></blockquote></div><br>