玩弄类型类我想出了看似无辜的
class Pair p a | p -> a where
one :: p -> a
two :: p -> a
这似乎工作正常,例如
instance Pair [a] a where
one [x,_] = x
two [_,y] = y
但是我遇到了元组的麻烦。即使以下定义编译...
instance Pair (a,a) a where
one p = fst p
two p = snd p
...我无法按预期使用它:
main = print $ two (3, 4)
No instance for (Pair (t, t1) a)
arising from a use of `two' at src\Main.hs:593:15-23
Possible fix: add an instance declaration for (Pair (t, t1) a)
In the second argument of `($)', namely `two (3, 4)'
In the expression: print $ two (3, 4)
In the definition of `main': main = print $ two (3, 4)
有没有办法正确定义实例?还是我必须求助于newtype
包装?