I have been going through the Seven Languages in Seven Weeks book and I'm working with Haskell.
I am struggling with the problem:
Write a sort that takes a list and a function that compares its two arguments and then returns a sorted list.
I searched online for help and found a solution but I can't even get the solution to run because of an expected to actual type error.
Here is the code I've been trying:
module Main where
import Data.List
sortList :: (Ord a) => (a -> a -> Ordering) -> [a] -> [a]
sortList comparator list = sortBy comparator list
Here is the error:
*Main> sortList [5,4,2,7,8,1]
<interactive>:1:10:
Couldn't match expected type `a -> a -> Ordering'
with actual type `[t]'
In the first argument of `sortList', namely `[5, 4, 2, 7, ....]'
In the expression: sortList [5, 4, 2, 7, ....]
In an equation for `it': it = sortList [5, 4, 2, ....]
My Thoughts and Attempts:
Maybe I am calling the function wrong? I am fairly new to Haskell. II tried doing many searches as well. All I could conclude is that somewhere the types aren't matching up. I suppose explanation and guidance for the script would be very helpful to me.
sortList
requires acomparator
you did not provide... – Willem Van Onsem