我已經定義了卡牌西服哈斯克爾
module Card (Suit(..), Rank(..), Card(..)) where
import Data.List
data Suit = Club | Diamond | Heart | Spade
deriving (Eq, Ord, Bounded, Enum)
suitchars = "CDHS"
data Rank =
R2 | R3 | R4 | R5 | R6 | R7 | R8 | R9 | R10 |
Jack | Queen | King | Ace
deriving (Eq, Ord, Bounded, Enum)
rankchars = "23456789TJQKA"
data Card = Card {suit::Suit, rank::Rank}
deriving (Eq, Bounded)
instance Ord Card where
compare (Card s1 r1) (Card s2 r2) =
let suitorder = compare s1 s2
in if suitorder == EQ then compare r1 r2 else suit order
,我嘗試使用
getSuit:: [Card] -> Suit
getSuit [Card s r] = Suit [Card s r]
得到我的卡的西裝,但它不工作,我也有一對牌列表
allCards :: [Card]
allCards = [Card suit rank | suit <- [Club .. Spade], rank <- [R2 .. Ace]]
cardPair :: [[Card]]
cardPair = [[c1, c2] | c1 <- allCards, c2 <- allCards, c1 < c2 ]
現在我想搬出去,或繼續從cardPair卡的配對,包含卡那件衣服,但我得到的問題
*「它不工作[...]我有問題」*。什麼不行?什麼問題?編譯器錯誤?運行時異常?你的貓着火了嗎?此外,ThreeFX提供了一個不錯的'cardPair'函數[上次](http://stackoverflow.com/questions/25600917/haskell-pair-two-cards),但它與你當前的問題有什麼關係? (其實,你的帖子無論如何都缺少一個問題) – Zeta 2014-09-11 11:59:33