简体   繁体   中英

Haskell vector type declaration

I'm writing a comparator to pass to sortBy but I can't get the type declaration right. The input is two Data.Vector 's, each containing two numbers.

-- 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:

Expected a constraint, but 'Vector a' has kind '*'

Vector is a data type, not a class, so your function type should be

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

When I changed this, it compiled for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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