枚举是表示卡片的好选择。您可以免费比较西装和等级之间的比较,并轻松地将枚举从/转换为int
.
type suit =
| Spade = 1
| Heart = 2
| Club = 3
| Diamond = 4
type rank =
| Ace = 1 | Two = 2 | Three = 3 | Four = 4 | Five = 5 | Six = 6 | Seven = 7
| Eight = 8 | Nine = 9 | Ten = 10 | Jack = 11 | Queen = 12 | King = 13
/// 'Card' is a type which represents a particular card
type Card = Card of rank * suit
/// 'deck' is a list consisting of all cards in a full deck
let deck = [ for r in 1..13 do
for s in 1..4 do
yield Card(enum<rank> r, enum<suit> s) ]
如果您选择有区别的联合,则必须手动制作所有suit
s 和所有rank
s 的列表。优点是 DU 的模式匹配比枚举更好。
type suit =
| Spade
| Heart
| Club
| Diamond
type rank = | Ace | Two | Three | Four | Five | Six | Seven
| Eight | Nine | Ten | Jack | Queen | King
type Card = Card of rank * suit
let private suits = [Spade; Heart; Club; Diamond]
let private ranks = [Ace; Two; Three; Four; Five; Six; Seven;
Eight; Nine; Ten; Jack; Queen; King]
let deck = [ for rank in ranks do
for suit in suits do
yield Card(rank, suit) ]