FINAL EDIT:
My final function is:
isPrime n = n > 1 && n < 4
|| n `mod` 2 /= 0
&& n `mod` 3 /= 0
&& length [x | x <- [5, 11..round (sqrt (fromIntegral n))], n `mod` x == 0 || n `mod` (x + 2) == 0] == 0
The issue was that I was trying to treat n
as both a Floating
in sqrt n
, and as an Integral
in n mod x
. So I had to do fromIntegral n
to force n to be treated as an Integral
and not as a Floating
.
Also I just screwed up some of my code.
So I have two functions:
primes = filterPrime [2..]
where filterPrime (p:xs) = p : filterPrime [x | x <- xs, x `mod` p /= 0]
above is straight off of haskell.org, an example being take 5 primes
results in [2,3,5,7,11]
My function isPrime
is the following:
isPrime n = isPrime n = n > 1 && n < 4
|| (n `mod` 2 == 0 || n `mod` 3 == 0)
&& length [x | x <- [5, 11..round (n ** 0.5)], n `mod` x == 0 || n `mod` (x + 2) == 0] == 0
However, when I call isPrime
I get the error:
EulerMath.hs:6:33: error:
* No instance for (RealFrac Int) arising from a use of `round'
* In the expression: round (n ** 0.5)
In the expression: [5, 11 .. round (n ** 0.5)]
In a stmt of a list comprehension: x <- [5, 11 .. round (n ** 0.5)]
|
6 | && length [x | x <- [5, 11..round (n ** 0.5)], n `mod` x == 0 || n `mod` (x + 2) == 0] == 0
| ^^^^^^^^^^^^^^^^
EulerMath.hs:6:40: error:
* No instance for (Floating Int) arising from a use of `**'
* In the first argument of `round', namely `(n ** 0.5)'
In the expression: round (n ** 0.5)
In the expression: [5, 11 .. round (n ** 0.5)]
|
6 | && length [x | x <- [5, 11..round (n ** 0.5)], n `mod` x == 0 || n `mod` (x + 2) == 0] == 0
| ^^^^^^^^
EulerMath.hs:6:45: error:
* No instance for (Fractional Int) arising from the literal `0.5'
* In the second argument of `(**)', namely `0.5'
In the first argument of `round', namely `(n ** 0.5)'
In the expression: round (n ** 0.5)
|
6 | && length [x | x <- [5, 11..round (n ** 0.5)], n `mod` x == 0 || n `mod` (x + 2) == 0] == 0
| ^^^
I don't know how to annotate the isPrime
function correctly (read: I don't know how to annotate properly at all) so I don't get this error