Im OOP /試圖學習haskell的命令式程序員,我正在開發一款紙牌遊戲作爲練習。發現在值中使用了哪個VALUE構造函數
此時,即時試圖因爲它遵循以定義庫甲板:
data Suit = Hearts
| Diamonds
| Spades
| Clubs
deriving(Show, Eq)
type CardValue = Int
data Card = Ace {cSuit :: Suit}
| Two {cSuit :: Suit}
| Three {cSuit :: Suit}
| Four {cSuit :: Suit}
| Five {cSuit :: Suit}
| Six {cSuit :: Suit}
| Seven {cSuit :: Suit}
| Eight {cSuit :: Suit}
| Nine {cSuit :: Suit}
| Ten {cSuit :: Suit}
| Jack {cSuit :: Suit}
| Queen {cSuit :: Suit}
| King {cSuit :: Suit}
deriving(Show, Eq)
type Hand = [Card]
type Deck = [Card]
cardValue :: Card -> Int
cardValue card =
| card.ValueConstructorName == Ace = 11
| card.ValueConstructorName == Seven = 10
| otherwise = 0
1)card.ValueConstructorName是幻想字段/方法/不管。有沒有一種方法來實現這個使用衛兵?
2)我真的需要在值構造函數的每一個選擇中都鍵入'{cSuit :: Suit}'嗎?
3)這種抽象牌組合是否合理?我的意思是,使用嵌套類型 感謝您