I am trying to implement selection sort but I am running into an issue whenever I use my own function minim
it crashes even tho that it also returns the same value as the minimum
function from prelude.
rmf :: Eq t => t -> [t] -> [t]
rmf _ [] = []
rmf oc (h : t)
| oc == h = t
|otherwise = h : rmf oc t
mymin :: Int -> Int -> Int
mymin a b
| a > b = b
| a < b = a
| a == b = a
minim :: [Int] -> Int
minim [a] = a
minim (h:t) = mymin h (minim t)
seqSort :: (Eq a, Ord a) => [a] -> [a]
seqSort [] = []
seqSort xs = s : seqSort (rmf s xs)
where s = minimum xs
main :: IO ()
main = print(seqSort [
CodePudding user response:
minim :: [Int] -> Int
This function works on lists of integers. Not on any other lists -- only lists of integers.
seqSort :: (Eq a, Ord a) => [a] -> [a]
This function works on lists of any ordered type. On lists of integers, but also lists of booleans, of strings, etc. Hence, this function can not use minim
which only works in a specific sub-case.
Solution: generalize the type of minim
accordingly. You'll have to also fix mymin
similarly.
As a final note, remember to post the error. Debugging this would have been much easier with the error at hand:
* Couldn't match type `a' with `Int'
Expected: [Int]
Actual: [a]
`a' is a rigid type variable bound by
the type signature for:
seqSort :: forall a. (Eq a, Ord a) => [a] -> [a]
at <source>:19:1-38
* In the first argument of `minim', namely `xs'
In the expression: minim xs
In an equation for `s': s = minim xs
* Relevant bindings include
xs :: [a] (bound at <source>:21:9)
seqSort :: [a] -> [a] (bound at <source>:20:1)
|
22 | where s = minim xs
| ^^
Note the part
Expected: [Int]
Actual: [a]
complaining that minim
only accepts [Int]
and won't take an [a]
. That's the issue.