I'm trying to prove that the numbers of the form p_1 * ... * p_k + 1
are not all prime, and to do this, I have written this code
sieve :: [Integer] -> [Integer]
sieve (0:xs) = sieve xs
sieve (x:xs) = x : sieve (mark x xs)
where
mark :: Integer -> [Integer] -> [Integer]
mark n (y:ys)
| rem y n == 0 = 0 : (mark n ys)
| otherwise = y : (mark n ys)
checkPrime' :: Integer -> Bool
checkPrime' n = elem n (sieve [n])
listPrimeFromTo' :: Integer -> Integer -> [Integer]
listPrimeFromTo' k n = sieve [k..n]
checkEulerPrimes = forall [(product xs) + 1| n <- [2..], let xs = listPrimeFromTo' 2 n] checkPrime'
I get this exception:
*** Exception: Ch3.hs:(11,2)-(13,31): Non-exhaustive patterns in function mark
However, in the definition of the function mark
, I use otherwise
, so how is it possible that there are cases which the definition of the function does not specifies any rule for that. I mean I thought using the keyword otherwise
makes sure that there is no non-exhausted patterns.
otherwise
is indeed a guard which always succeeds; but guards are only considered when the associated pattern already matches. So, in
foo (x:xs) | otherwise = bar
we will only see bar
as the result when the argument to foo
matches the pattern x:xs
. The analogous pattern to otherwise
is _
, which always matches and doesn't bind any new variables, so:
foo (x:xs) | otherwise = bar
foo _ = baz
would never throw an unmatched pattern exception.