data Suit = Spade | Heart | Club | Diamond deriving (Eq, Ord, Enum, Show)
data Pip = Ace | Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten | Jack | Queen | King deriving (Eq, Ord, Enum, Show)
data Card = Card
{suit :: Suit
,pip :: Pip
} deriving (Eq,Ord, Show)
type Deck = [Card]
data EOBoard = EOBoard
{foundations :: [Deck]
,columns :: [Deck]
,reserve :: [Deck]
} deriving (Eq, Ord, Show)
instance Enum Card where
这是纸牌数据结构
如何使用Card
实例使Enum
可枚举
Card{suit = Spade, pip = Ace}
,Card{suit = Spade, pip = Two}
.. Card{suit = Heart, pip = Ace}
.. Card{suit = Club, pip = Ace}
.. Card{suit = Diamond, pip = Ace} ..Card{suit = Diamond, pip = King}
。
答案 0 :(得分:5)
这是我将如何做到的(基本上是@ epsilonhalbe的想法):
data Suit
= Spade | Heart | Club | Diamond
deriving (Eq, Ord, Enum, Show, Bounded)
data Pip
= Ace | Two | Three | Four | Five | Six | Seven | Eight | Nine
| Ten | Jack | Queen | King
deriving (Eq, Ord, Enum, Show, Bounded)
data Card = Card
{ suit :: Suit
, pip :: Pip
} deriving (Eq,Ord, Show, Bounded)
instance Enum Card where
toEnum i =
let (d,m) = i `divMod` (fromEnum (maxBound :: Pip) + 1)
in Card (toEnum d) (toEnum m)
fromEnum (Card s p) = fromEnum s * (fromEnum (maxBound :: Pip) + 1) + fromEnum p
这会给你:
λ> take 5 $ [minBound .. maxBound] :: [Card]
[Card {suit = Spade, pip = Ace},Card {suit = Spade, pip = Two},Card {suit = Spade, pip = Three},Card {suit = Spade, pip = Four},Card {suit = Spade, pip = Five}]
PS:现在你应该全力以赴 - 对不起我搞砸了maxBound
忘记了一个额外的