[Haskell-beginners] Problem with numeric types

Russ Abbott russ.abbott at gmail.com
Sat Jul 2 01:41:56 UTC 2016


I'm trying to define primes using just list comprehension.  The following
works.

Prelude> odds = [3, 5..]

Prelude> primes = 2 : [p | p <- odds, null [d | d <- take (p `div` 4) odds,
p `mod` d == 0]]


But when I replace  "take (p `div` 4)"   with    takeWhile (<=(sqrt p))  as
in

Prelude> primes' = 2 : [p | p <- odds, null [d | d <- (takeWhile (<=(sqrt
p)) odds), p `mod` d == 0]]

I get no error message on entering the statement, but when I run

Prelude> take 8 primes'

I get an error message that I can't understand.

<interactive>:23:1: error:
    • Ambiguous type variable ‘a0’ arising from a use of ‘it’
      prevents the constraint ‘(Floating a0)’ from being solved.
      Probable fix: use a type annotation to specify what ‘a0’ should be.
      These potential instances exist:
        instance Floating Double -- Defined in ‘GHC.Float’
        instance Floating Float -- Defined in ‘GHC.Float’
    • In the first argument of ‘print’, namely ‘it’
      In a stmt of an interactive GHCi command: print it

I tried using (ceiling (sqrt p)), and I tried to explicitly declare it an
Int, but neither of those helped.

I'd appreciate some help.

Thanks.

P.S. The following runs fine.

Prelude> takeWhile (<= (sqrt 99)) odds

So I'm completely stumped.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160702/d97acbe1/attachment.html>


More information about the Beginners mailing list