0
\$\begingroup\$

I originally had the following code:

type Suit = | Spades
            | Clubs
            | Diamonds
            | Hearts

type Face = | Two  | Three | Four  | Five
            | Six  | Seven | Eight | Nine | Ten
            | Jack | Queen | King  | Ace

type Card = { Face:Face; Suit:Suit }

let suits = [Spades; Clubs; Diamonds; Hearts]
let faces = [Two; Three; Four; Five; Six; Seven; Eight; Nine; Ten;
             Jack; Queen; King; Ace]

let deck = [for suit in suits do
                for face in faces do
                    yield {Face=face; Suit=suit}]

The output was:

val deck : Card list =
  [{Face = Two;
    Suit = Spades;}; {Face = Three;
                      Suit = Spades;}; {Face = Four;
                                        Suit = Spades;}; {Face = Five;
                                                          Suit = Spades;};
   {Face = Six;
    Suit = Spades;}; {Face = Seven;
                      Suit = Spades;}; {Face = Eight;
                                        Suit = Spades;}; {Face = Nine;
                                                          Suit = Spades;};
   {Face = Ten;
    Suit = Spades;}; {Face = Jack;
                      Suit = Spades;}; {Face = Queen;
                                        Suit = Spades;}; {Face = King;
                                                          Suit = Spades;};
   {Face = Ace;
    Suit = Spades;}; {Face = Two;
                      Suit = Clubs;}; {Face = Three;
                                       Suit = Clubs;}; {Face = Four;
                                                        Suit = Clubs;};
   {Face = Five;
    Suit = Clubs;}; {Face = Six;
                     Suit = Clubs;}; {Face = Seven;
                                      Suit = Clubs;}; {Face = Eight;
                                                       Suit = Clubs;};
   {Face = Nine;
    Suit = Clubs;}; {Face = Ten;
                     Suit = Clubs;}; {Face = Jack;
                                      Suit = Clubs;}; {Face = Queen;
                                                       Suit = Clubs;};
   {Face = King;
    Suit = Clubs;}; {Face = Ace;
                     Suit = Clubs;}; {Face = Two;
                                      Suit = Diamonds;}; {Face = Three;
                                                          Suit = Diamonds;};
   {Face = Four;
    Suit = Diamonds;}; {Face = Five;
                        Suit = Diamonds;}; {Face = Six;
                                            Suit = Diamonds;};
   {Face = Seven;
    Suit = Diamonds;}; {Face = Eight;
                        Suit = Diamonds;}; {Face = Nine;
                                            Suit = Diamonds;};
   {Face = Ten;
    Suit = Diamonds;}; {Face = Jack;
                        Suit = Diamonds;}; {Face = Queen;
                                            Suit = Diamonds;};
   {Face = King;
    Suit = Diamonds;}; {Face = Ace;
                        Suit = Diamonds;}; {Face = Two;
                                            Suit = Hearts;}; {Face = Three;
                                                              Suit = Hearts;};
   {Face = Four;
    Suit = Hearts;}; {Face = Five;
                      Suit = Hearts;}; {Face = Six;
                                        Suit = Hearts;}; {Face = Seven;
                                                          Suit = Hearts;};
   {Face = Eight;
    Suit = Hearts;}; {Face = Nine;
                      Suit = Hearts;}; {Face = Ten;
                                        Suit = Hearts;}; {Face = Jack;
                                                          Suit = Hearts;};
   {Face = Queen;
    Suit = Hearts;}; {Face = King;
                      Suit = Hearts;}; {Face = Ace;
                                        Suit = Hearts;}]
 

However, the code above did not account for jokers.

So I updated the code to the following:

type Suit = 
    | Spades | Hearts 
    | Clubs  | Diamonds

type Face =
    | Ace  | King  | Queen | Jack | Ten
    | Nine | Eight | Seven | Six  | Five | Four | Three | Two

type Joker = BigJoker | LittleJoker

type Standard = { Face:Face; Suit:Suit }

and Card = 
    | Card of Standard 
    | Wild of Joker

let suits = [Spades; Hearts; Clubs; Diamonds]

let faces = [Ace; King; Queen; Jack; Ten; Nine; Eight; Seven
             Six; Five; Four; Three; Two]

let deck = [for suit in suits do
                for face in faces do
                    yield Card { Face=face; Suit=suit }
           ] @ [Wild BigJoker
                Wild LittleJoker]

The output is now:

val deck : Card list =
  [Card {Face = Ace;
         Suit = Spades;}; Card {Face = King;
                                Suit = Spades;}; Card {Face = Queen;
                                                       Suit = Spades;};
   Card {Face = Jack;
         Suit = Spades;}; Card {Face = Ten;
                                Suit = Spades;}; Card {Face = Nine;
                                                       Suit = Spades;};
   Card {Face = Eight;
         Suit = Spades;}; Card {Face = Seven;
                                Suit = Spades;}; Card {Face = Six;
                                                       Suit = Spades;};
   Card {Face = Five;
         Suit = Spades;}; Card {Face = Four;
                                Suit = Spades;}; Card {Face = Three;
                                                       Suit = Spades;};
   Card {Face = Two;
         Suit = Spades;}; Card {Face = Ace;
                                Suit = Hearts;}; Card {Face = King;
                                                       Suit = Hearts;};
   Card {Face = Queen;
         Suit = Hearts;}; Card {Face = Jack;
                                Suit = Hearts;}; Card {Face = Ten;
                                                       Suit = Hearts;};
   Card {Face = Nine;
         Suit = Hearts;}; Card {Face = Eight;
                                Suit = Hearts;}; Card {Face = Seven;
                                                       Suit = Hearts;};
   Card {Face = Six;
         Suit = Hearts;}; Card {Face = Five;
                                Suit = Hearts;}; Card {Face = Four;
                                                       Suit = Hearts;};
   Card {Face = Three;
         Suit = Hearts;}; Card {Face = Two;
                                Suit = Hearts;}; Card {Face = Ace;
                                                       Suit = Clubs;};
   Card {Face = King;
         Suit = Clubs;}; Card {Face = Queen;
                               Suit = Clubs;}; Card {Face = Jack;
                                                     Suit = Clubs;};
   Card {Face = Ten;
         Suit = Clubs;}; Card {Face = Nine;
                               Suit = Clubs;}; Card {Face = Eight;
                                                     Suit = Clubs;};
   Card {Face = Seven;
         Suit = Clubs;}; Card {Face = Six;
                               Suit = Clubs;}; Card {Face = Five;
                                                     Suit = Clubs;};
   Card {Face = Four;
         Suit = Clubs;}; Card {Face = Three;
                               Suit = Clubs;}; Card {Face = Two;
                                                     Suit = Clubs;};
   Card {Face = Ace;
         Suit = Diamonds;}; Card {Face = King;
                                  Suit = Diamonds;}; Card {Face = Queen;
                                                           Suit = Diamonds;};
   Card {Face = Jack;
         Suit = Diamonds;}; Card {Face = Ten;
                                  Suit = Diamonds;}; Card {Face = Nine;
                                                           Suit = Diamonds;};
   Card {Face = Eight;
         Suit = Diamonds;}; Card {Face = Seven;
                                  Suit = Diamonds;}; Card {Face = Six;
                                                           Suit = Diamonds;};
   Card {Face = Five;
         Suit = Diamonds;}; Card {Face = Four;
                                  Suit = Diamonds;}; Card {Face = Three;
                                                           Suit = Diamonds;};
   Card {Face = Two;
         Suit = Diamonds;}; Wild BigJoker; Wild LittleJoker]
\$\endgroup\$
3
\$\begingroup\$

Not sure what sort of feedback you're looking for since you didn't include any questions, but your deck-building can be made a little more efficient:

let deck = [
    for suit in suits do
        for face in faces do
            yield Card { Face=face; Suit=suit }
    yield Wild BigJoker
    yield Wild LittleJoker
]

The original version made two lists and used the @ operator to concatenate them into a third list, while this version just creates a single list.

Jokers aren't always the only wild card, depending on the game you're playing. I might personally hesitate to encode into the type system that only Jokers are wild.

One other option might be...

and Card = 
    | Card of Standard 
    | Joker of Joker

...or possibly you could make Joker a suit, and BigJoker/LittleJoker face values in that suit. But then the type system wouldn't prevent you from creating the BigJoker of Spades, which would be a little weird.

\$\endgroup\$
  • 1
    \$\begingroup\$ Also possibly a good idea: and Card = | Card of Standard | LittleJoker | BigJoker Might flatten out the match c with ... sections later on. \$\endgroup\$ – BitTickler Jan 7 '17 at 12:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.