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!!
Delete all the return
keywords:
howManyEqual :: Int -> Int -> Int -> Integer
howManyEqual x y z =
if x == y && x == z then 3
else if x == y && x /= z then 2
else if x == z && x /= y then 2
else if y == z && y /= x then 2
else 0
In Haskell, return
isn't a keyword, but a function with the type Monad m => a -> m a
. It's mainly used from within code blocks in do
notation to return values 'wrapped' in a Monad
instance.
The howManyEqual
method doesn't return a monadic value, but rather a normal, scalar Integer
, so return
isn't required. When you use return
anyway, the compiler expects the returned value to be part of some Monad
instance that it calls m0
in the error message, but since the function has the explicitly declared return type Integer
, there's no way to reconcile these.
By the way, you can relax the type declaration to:
howManyEqual :: (Eq a, Num t) => a -> a -> a -> t
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With