![](/img/trans.png)
[英]Transform a function with a typeclass constraint into a function taking an explicit typeclass dictionary
[英]How to deal with typeclass constraint in class member function
GHCI抱怨的類型k
和a
內部(k * a, k * b)
我的猜測是它不知道k
是一個Num
但我不知道如何指定它。
module Point where
data Point x y = Point (x, y)
class Vector v where
add :: v -> v -> v
sub :: v -> v -> v
mul :: Num k => k -> v -> v
instance (Num a, Num b) => Vector (Point a b) where
add (Point (a, b)) (Point (c, d)) = Point (a + c, b + d)
sub (Point (a, b)) (Point (c, d)) = Point (a - c, b - d)
mul k (Point (a, b)) = Point (k * a, k * b)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.