我正在学习 Haskell,并且收到了以下作业 - 我有一个由两种混合数据类型组成的新类型,我必须将其设为 Eq 的实例,而不使用派生。这是我所拥有的:
data Number = One | Two | Three deriving Eq
data Character = A | B | C deriving Eq
newtype Combo = Combo ((Character, Character),(Number, Number))
instance Eq Combo where
Combo ((a1, a2),(x1,x2)) == Combo ((b1, b2),(y1, y2)) = (a1 == b1) && (a2 == b2) && (x1 == y1) && (x2 == y2)
然而,拥抱才是一切
ERROR "testing.hs":5 - Ambiguous class occurrence "Eq"
*** Could refer to: Hugs.Prelude.Eq Main.Eq Main.Eq Main.Eq Main.Eq Main.Eq
我该如何解决这个问题?我无法真正导入隐藏某些内容的 Eq,因为我需要它来检查 Number 或 Character 的给定成员是否相等。
最佳答案
改变
instance Eq Combo where
Combo ((a1, a2),(x1,x2)) == Combo ((b1, b2),(y1, y2)) = (a1 == b1) && (a2 == b2) && (x1 == y1) && (x2 == y2)
至
instance Main.Eq Combo where
Combo ((a1, a2),(x1,x2)) == Combo ((b1, b2),(y1, y2)) = (a1 == b1) && (a2 == b2) && (x1 == y1) && (x2 == y2)
显然已修复该错误。
关于haskell - 创建 Eq 的新类型实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33784541/