I was doing some test functions to understand a little more about Haskell, and I did this:
constroiMSet :: Ord a => [a] -> [(a,Int)]
constroiMSet( x:x1:xs) |x1==x =[(x,2)]
The thing I don't get is why this function works. This function has Ord a
but it uses ==
to compare a
. Isn't Ord a
only for >=
, <=
, <
, >
?
If we look at the definition of the Ord
class, we see:
class Eq a => Ord a where
compare :: a -> a -> Ordering
(<) :: a -> a -> Bool
(<=) :: a -> a -> Bool
(>) :: a -> a -> Bool
(>=) :: a -> a -> Bool
max :: a -> a -> a
min :: a -> a -> a
So this means that every type a
where Ord a
, also means Eq a
. So an a
that supports Ord
, also supports Eq
and thus (==)
and (/=)
.
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