下午好,
我是 Haskell 的新手,我正在尝试调整我制作的 QuickSort 算法,以使用元组列表对“Int”列表进行排序,但我完全不知道如何将尾部绑定到“a”让它按照我的需要工作,或者是否可以重用代码。这是我用来对“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
这是我试图用它做的,所以我可以对元组列表(Int,[Int])进行排序。我想按元组的第一个元素对元组进行排序,所以如果我得到一个类似 [(2,[1]),(1,[]),(3,[2,1])] 的列表,它会返回这个[(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
如果我尝试加载它,我会收到以下错误:
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)
非常感谢您能给我的任何见解。