Assigning a type signature as a variable

Let's say I have a sorting algorithm and two custom comparators:

mySort :: [[Int]] -> ([Int] -> [Int] -> Ordering) -> [[Int]] mySort = undefined myCmp1 :: [Int] -> [Int] -> Ordering myCmp1 xs ys | a0 < b0 = LT | a0 > b0 = GT | a1 < b1 = LT | a1 > b1 = GT | a1 == b1 = EQ where a0 = head xs b0 = head ys a1 = last xs b1 = last ys myCmp2 :: [Int] -> [Int] -> Ordering myCmp2 xs ys | a0 > b0 = LT | a0 < b0 = GT | a1 > b1 = LT | a1 < b1 = GT | a1 == b1 = EQ where a0 = head xs b0 = head ys a1 = last xs b1 = last ys 

Is there a way to define a signature like [Int] -> [Int] -> Ordering so that I can use it like this:

 comparator = [Int] -> [Int] -> Ordering mySort :: [[Int]] -> comparator -> [[Int]] mySort = undefined 
+6
source share
1 answer

This is what type aliases do:

 type Comparator = [Int] -> [Int] -> Ordering 

They can also take arguments:

 type Comparator a = a -> a -> Ordering 

Then you can write, for example.

 mySort :: Comparator a -> [a] -> [a] 
+12
source

All Articles