[英]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抱怨:
預計有一個約束,但'Vector a'有點'*'
Vector
是一種數據類型,而不是類,所以你的函數類型應該是
indCmp :: (Ord a, Num a) => Vector a -> Vector a -> Ordering
當我改變它時,它為我編譯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.