FCards - Defining Cards

06. Picking up a card

Picking up a card

Now that we have a deck, we can pick a card up and add it to our hand

Defining a function

In F# a function is just another kind of variable, but one that is derived from another value(s), so we use the let keyword. Also, as it’s just a kind of value, they can be passed into other functions too.

let add a b = a + b

let multiply a b = a * b

let combine combiner a b =
  combiner (a + 1) (b * 2)

// to use it...
let added = combine add 3 4  // should equal 12
let multiplied = combine multiply 3 4  // should equal 32

TIP: there is no return keyword. The last result calculated is the value that is returned

Exercise:

Define a function that takes the top card from the deck and puts it in our hand.

See an answer

let pickupCard (hand: Card list) (deck: Card list) =
  if deck.Length = 0 then 
    failwith "No cards left!!"
  else
    let topcard = deck[0]
    hand @ [topcard]

let hand = [Hearts Three; Diamonds Ten; Clubs King]
let updatedHand = pickupCard hand aNewDeck

"""
Note that we can be more specific about the types of the function's inputs as `(label: type)`.  If we don't do this the compiler tries to figure it out.  Most of the time the compiler's pretty good at that.
"""

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

let pickupCard (hand: Card list) (deck: Card list) =
  if deck.Length = 0 then 
    failwith "No cards left!!"
  else
    let topcard = deck[0]
    hand @ [topcard]

;;
// DO IT!
pickupCard hand newDeck