Pattern matching error in haskell -
hello have been attempting answer question deals checking if number prime number. came code below:
isitprime :: int->bool isitprime n | n<=1 = false | otherwise = isitprime2 n (n-1) isitprime2 :: int->int->bool isitprime2 x y | y > 1 && x `mod` y == 0 = false | y == 1 && x `mod` y == 0 = true
when run in winhugs, returns error saying "pattern matching failure: isitprime2 a-1 " value greater 2.
however returns false values directly multiplied 2, e.g. isitprime2 2 1 returns false, isitprime2 4 2, isitprime2 6 3, isitprime2 10 5 etc. return false.
what going wrong , why?
thanks :d
the problem function isitprime2
doesn't handle cases. happens when:
y > 1 && x mod y != 0
y == 1 && x mod y != 0
handle other cases , code work fine. can use otherwise
clause handling edge case:
isitprime2 :: int->int->bool isitprime2 x y | y > 1 && x `mod` y == 0 = false | y == 1 && x `mod` y == 0 = true | otherwise = ??? -- handle edge case
Comments
Post a Comment