私はこのハスケルコードを持っています。Mord
2 つのデータ型を作成したので、関数と型を比較できる新しいクラスを作成したいと考えてい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.