Good afternoon,
I'm a newb to Haskell, and I'm trying to adapt a QuickSort algorithm I made to sort lists of 'Int' with a list of tuples, but I quite can't figure out how to bind the tail to 'a' to make it work as I need it to, or if it's possible to reuse the code at all. Here's what I've been using to sort lists of 'Int':
quickSort :: Ord a => [a] -> [a]
quickSort [] = []
quickSort (x:xs) =
let smallSort = quickSort [a | a <- xs, a < x]
biggerSort = quickSort [a | a <- xs, a > x]
in smallSort ++ [x] ++ biggerSort
And here is what I've tried to do with it so I could sort lists of tuples (Int,[Int]). I want to sort the tuples by the first element of the tuple, so if I get a lista like [(2,[1]),(1,[]),(3,[2,1])] it returns this [(1,[]),(2,[1]), (3,[2,1])].
quickSortTuplas ((x,(ks)) : []) = [(x,(ks))]
quickSortTuplas ((x,(ks)):ps) =
let smallSort = quickSort [a | a <- ps, a < x]
biggerSort = quickSort [a | a <- ps, a > x]
in smallSort ++ [(x,(ks))] ++ biggerSort
If I try to load this, I get the following error:
Occurs check: cannot construct the infinite type: a ~ (a, [a1])
* In the second argument of `(>)', namely `x'
In the expression: a > x
In a stmt of a list comprehension: a > x
* Relevant bindings include
a :: (a, [a1]) (bound at reNuevoHaskell.hs:60:37)
biggerSort :: [(a, [a1])] (bound at reNuevoHaskell.hs:60:9)
ps :: [(a, [a1])] (bound at reNuevoHaskell.hs:58:27)
ks :: [a1] (bound at reNuevoHaskell.hs:58:22)
x :: a (bound at reNuevoHaskell.hs:58:19)
quickSortTuplas :: [(a, [a1])] -> [(a, [a1])]
(bound at reNuevoHaskell.hs:57:1)
Many thanks in advance for any insight you could give me.
notice in the expresion [a | a <- ps, a < x]
. a
is a tuple whereas x
is an Int
. Hence a < x
makes no sense. Any case, because your quicksort
works on Ord a
, you can use it for ordering a list of tuples as well. Try it`!
quickSort :: Ord a => [a] -> [a]
quickSort [] = []
quickSort (x:xs) =
let smallSort = quickSort [a | a <- xs, a < x]
biggerSort = quickSort [a | a <- xs, a > x]
in smallSort ++ [x] ++ biggerSort
main = print $ quickSort [(1,[2,3,4]) , (0, [4,5,6]), (2,[1,2,3])] -- This works fine