<div dir="ltr"><div><div>Because mod :: Integral a => a -> a -> a and sqrt :: Floating a => a -> a, the final type of primes' ends up being :: (Floating a, Integral a) => [a], which doesn't work very well obviously.<br><br></div>The easiest solution is to try<br>let primes' = 2 : [p | p <- odds, null [d | d <- (takeWhile (<=ceiling (sqrt (fromInteger p))) odds), p `mod` d == 0]]<br><br></div>You can make your own sqrt if you want to clean it up.<br>let isqrt = ceiling . sqrt . fromInteger<br><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jul 1, 2016 at 9:41 PM, Russ Abbott <span dir="ltr"><<a href="mailto:russ.abbott@gmail.com" target="_blank">russ.abbott@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">I'm trying to define primes using just list comprehension. The following works.<div><br></div><div>Prelude> odds = [3, 5..]<br><div><br></div><div><div>Prelude> primes = 2 : [p | p <- odds, null [d | d <- take (p `div` 4) odds, p `mod` d == 0]]</div></div><div><br></div><div><br></div><div>But when I replace "take (p `div` 4)" with takeWhile (<=(sqrt p)) as in</div></div><div><br></div><div><div>Prelude> primes' = 2 : [p | p <- odds, null [d | d <- (takeWhile (<=(sqrt p)) odds), p `mod` d == 0]]</div></div><div><br></div><div>I get no error message on entering the statement, but when I run</div><div><br></div><div><div>Prelude> take 8 primes'</div></div><div><br></div><div>I get an error message that I can't understand. </div><div><br></div><div><div><interactive>:23:1: error:</div><div> • Ambiguous type variable ‘a0’ arising from a use of ‘it’</div><div> prevents the constraint ‘(Floating a0)’ from being solved.</div><div> Probable fix: use a type annotation to specify what ‘a0’ should be.</div><div> These potential instances exist:</div><div> instance Floating Double -- Defined in ‘GHC.Float’</div><div> instance Floating Float -- Defined in ‘GHC.Float’</div><div> • In the first argument of ‘print’, namely ‘it’</div><div> In a stmt of an interactive GHCi command: print it</div></div><div><br></div><div>I tried using (ceiling (sqrt p)), and I tried to explicitly declare it an Int, but neither of those helped.</div><div><br></div><div>I'd appreciate some help.</div><div><br></div><div>Thanks.</div><div><br></div><div>P.S. The following runs fine.</div><div><br></div><div><div>Prelude> takeWhile (<= (sqrt 99)) odds</div></div><div><br></div><div>So I'm completely stumped.</div><div><br></div><div><br></div><div><br></div></div>
<br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div>