I'm creating a merge sort function in Haskell that is recursive. I have been told as part of my assessment that I must define the type as:
isort :: Ord a => [a] -> [a]
I assumed the function expects an array as input and outputs an array. I know that ord
is a class.
In the context above what does the Ord a
mean?
This is my function:
isort :: Ord a => [a] -> [a]
isort [x] = [x]
isort (x:xs) = insert x (isort xs)
where
insert :: Int -> [Int] -> [Int]
insert a [] = []
insert a (b:c) | a < b = a:b:c
| otherwise = b : insert a c
When I try to load my functions file into ghci I get the error:
Couldn't match type ‘a’ with ‘Int’
‘a’ is a rigid type variable bound by
the type signature for isort :: Ord a => [a] -> [a]
at LabSheet2.hs:17:10
Expected type: [a]
Actual type: [Int]
...
The Ord a
is a typeclass constraint that indicates your function works for any type a
, so long as a
is comparable (Ord
erable). The error message you're getting is due to the conflict between your outer declaration that says it works for any Ord a => a
, and the inner insert
that "only" works for Int
's.
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