FCards - Defining Cards

05. A deck of cards

Creating a deck of cards

To pick up a card, we will need a deck of cards to pick from.

Exercise:

Define a thing that has all the cards in the deck as a list

There are some useful list functions that are supplied in the base libraries :

For List.map you will need to define an inline function. This takes the form
(fun x -> [calculate something here using x]).

So for List.map you might do something like this:

  let listOfTuples = [ (1,2) ; (3,4) ]
  List.map (fun (a,b) -> a + b) listOfTuples

See an answer

let newDeck = 
  // Note: this is a 'calculated value' as it takes no inputs.
  //  So, once this value is calculated the first time then it 
  //  just keeps the value forever.
  let suits = [Hearts; Diamonds; Clubs; Spades]
  let numbers = [
    Two; Three; Four; Five; Six; Seven; Eight; Nine; Ten;
    Jack; Queen; King; Ace
  ]
  let paired = List.allPairs suits numbers
  List.map (fun (suit, number) -> suit number) paired

Code so far

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

type Card = 
  | Hearts of CardNumber
  | Diamonds of CardNumber
  | Clubs of CardNumber
  | Spades of CardNumber
  | Joker

let hand = [Hearts Three; Diamonds Ten; Clubs King; Joker]

let newDeck = 
  // Note: this is a 'calculated value' as it takes no inputs.
  //  So, once this value is calculated the first time then it 
  //  just keeps the value forever.
  let suits = [Hearts; Diamonds; Clubs; Spades]
  let numbers = [
    Two; Three; Four; Five; Six; Seven; Eight; Nine; Ten;
    Jack; Queen; King; Ace
  ]
  let paired = List.allPairs suits numbers
  List.map (fun (suit, number) -> suit number) paired

;;
// DO IT!
newDeck