如何使自定义数据类型可订购?
我有一个Haskell的自定义数据类型,我想作为一个键使用Data.Map,Data.Graph以及其他的查找表。
data State = State
{ playerIdx :: Int
, piles :: [Int]
} deriving Show
我如何使它可订购?以下似乎不起作用:
data State = State
{ playerIdx :: Int
, piles :: [Int]
} deriving (Show, Ord)
回答
如果您将某物作为 的实例Ord,则它也需要是 的实例Eq,因此您应该同时派生Eq和Ord:
data State = State
{ playerIdx :: Int
, piles :: [Int]
} deriving (Eq, Ord, Show)
- Thanks Willem, presumably the OP would get an error message saying "No instance for (Eq State) arising from the 'deriving' clause ..."(?) So the lesson for @MikeRand is: don't just say "doesn't seem to work", but give us the error reported. And read that first -- it might give you the answer all on its own.