So I'm new to Haskell and I'm writing a basic function which takes 3 integers as arguments and returns how many are equal.
howManyEqual :: Int->Int->Int->Integer
howManyEqual x y z =
if x == y && x == z then return 3
else if x == y && x /= z then return 2
else if x == z && x /= y then return 2
else if y == z && y /= x then return 2
else return 0
But Im getting the following error:
Prelude> :load ex4.1_2.hs
[1 of 1] Compiling Main ( ex4.1_2.hs, interpreted )
ex4.1_2.hs:11:34:
Couldn't match expected type ‘Integer’
with actual type ‘m0 Integer’
In the expression: return 3
In the expression:
if x == y && x == z then
return 3
else
if x == y && x /= z then
return 2
else
if x == z && x /= y then
return 2
else
if y == z && y /= x then return 2 else return 0
I have same error at return 2
and return 0
as well.
What kind of data type is m0 Integer
and what do I need to do to fix this?
Any help would be appreciated. Cheers!!
/=
checks could be removed. You already know, because the first check failed, that the three numbers are not all equal. Therefore, if (for example)x == y
, then you already know thatx /= z
and do not need to test it. – amalloyhowManyEqual x y z | and xyz = 3 | or xyz = 2 | otherwise = 0 where xyz = [x == y, x == z, y == z]
—that is, if all of them compare equal, then 3 are equal; if only some of them compare equal, then 2 are equal; otherwise 0 are equal. – Jon Purdy