Haskell 向量类型声明
Haskell vector type declaration
我正在编写一个比较器以传递给 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 抱怨:
Expected a constraint, but 'Vector a' has kind '*'
Vector
是数据类型,不是class,所以你的函数类型应该是
indCmp :: (Ord a, Num a) => Vector a -> Vector a -> Ordering
当我更改它时,它会为我编译。
我正在编写一个比较器以传递给 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 抱怨:
Expected a constraint, but 'Vector a' has kind '*'
Vector
是数据类型,不是class,所以你的函数类型应该是
indCmp :: (Ord a, Num a) => Vector a -> Vector a -> Ordering
当我更改它时,它会为我编译。