简体   繁体   English

Haskell矢量类型声明

[英]Haskell vector type declaration

I'm writing a comparator to pass to sortBy but I can't get the type declaration right. 我正在编写一个比较器来传递给sortBy但我无法获得类型声明。 The input is two Data.Vector 's, each containing two numbers. 输入是两个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 complains: GHCI抱怨:

Expected a constraint, but 'Vector a' has kind '*' 预计有一个约束,但'Vector a'有点'*'

Vector is a data type, not a class, so your function type should be Vector是一种数据类型,而不是类,所以你的函数类型应该是

indCmp :: (Ord a, Num a)  => Vector a -> Vector a -> Ordering

When I changed this, it compiled for me. 当我改变它时,它为我编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM