I am working on an F# tutorial that creates a deck of cards. The types are listed, but I cannot understand how to loop through the types to create the map of the full deck. I expected to do something like
Foreach rank in ranks
Foreach suit in suits
somehow combine the two
next suit
next rank
Is there no way to do this? Below are the types created.
I think if I changed them from types to lists they could union, right? So, what’s the point of types?
type suits=
|Spade=1
|Heart=2
|Club=3
|Diamond=4
type ranks=
|ValCard of int
|Jack
|Queen
|King
type deck= Deck of ranks * suits
Enums is a good choice for representing cards. You have comparison among suits and among ranks for free, and easily convert enums from/to
int.If you go for discriminated unions, you have to manually make lists of all
suits and allranks. The advantage is better pattern matching of DUs than that of enums.