我正在编写一个传递给sortBy
的比较器,但我无法获得正确的类型声明。输入是两个Data.Vector
,每个包含两个数字。
-- Comparator to sort a list of individuals by increasing order of fit-0
-- and for individuals with equal fit-0, with increasing order of fit-1
indCmp :: (Ord a, Num a, Vector a) => a -> a -> Ordering
indCmp x y
| (x ! 0) < (y ! 0) = LT
| (x ! 0) > (y ! 0) = GT
| (x ! 1) < (y ! 1) = LT -- Can assume (x ! 0) == (y ! 0) here and beneath
| (x ! 1) > (y ! 1) = GT
| (x ! 1) == (y ! 1) = EQ
GHCI抱怨:
预计有一个约束,但'Vector a'有种'*'
答案 0 :(得分:3)
Vector
是数据类型,而不是类,因此您的函数类型应为
indCmp :: (Ord a, Num a) => Vector a -> Vector a -> Ordering
当我改变它时,它为我编译。