我有这个haskell代码。我已经创建了两种数据类型,然后我想创建一个Mord
可以将函数与Mlist
类型进行比较的新类。
import Data.List
data Mlist a = Mlist [a]
data Mordering = MLT deriving (Eq, Show)
s = Mlist [1, 2, 3]
t = Mlist [1, 4, 2, 3]
class Mord a where
mcompare :: a -> a -> Mordering
instance Mord a => Mord (Mlist a) where
mcompare (Mlist xs) (Mlist ys) = MLT
但如果我尝试mcompare s t
我得到
<interactive>:1:1:
No instance for (Mord Integer)
arising from a use of `mcompare'
Possible fix: add an instance declaration for (Mord Integer)
In the expression: mcompare s t
In an equation for `it': it = mcompare s t
有没有人看到问题?
编辑:
这是我的新代码:
import Data.List
data Mlist a = Mlist [a]
data Mordering = MEQ | MIN deriving (Eq, Show)
s = Mlist [1, 2, 3]
t = Mlist [1, 4, 2, 3]
class Mord a where
mcompare :: a -> a -> Mordering
instance Mord (Mlist a) where
mcompare (Mlist xs) (Mlist ys)
| length xs == length ys && null (xs \\ ys) = MEQ
| otherwise = MIN
但我现在得到的错误是:
No instance for (Eq a)
arising from a use of `\\'
In the first argument of `null', namely `(xs \\ ys)'
In the second argument of `(&&)', namely `null (xs \\ ys)'
In the expression: length xs == length ys && null (xs \\ ys)
Failed, modules loaded: none.