I just don't understand what the compiler is trying to tell me here:
let someCard = getFirstCard hand
Error:
This expression was expected to have type
Card * Card
but here has type
(Card * Card) option
I then tried this:
let getFirstCard (hand:Card*Card) = function
| card1, card2 -> Some(card1)
But I get the same error.
I just don't get the Options template. I continue to struggle with this.
My code is below:
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}
type Deal = | Hand of Card * Card
| Hit of Card
let private suits = [Spades; Clubs; Diamonds ; Hearts]
let private 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}]
let hitPlayer (deck:Card list) =
(deck.Head, deck.Tail)
let shuffle xs =
let swap i j (array : _[]) =
let tmp = array.[i]
array.[i] <- array.[j]
array.[j] <- tmp
let rnd = System.Random()
let xArray = Seq.toArray xs
let n = Array.length xArray
for i in [0..(n-2)] do
let j = rnd.Next(i, n-1)
swap i j xArray
xArray |> Seq.toList
let deal = function
| card1::card2::remaining -> Some(card1, card2), remaining
| _ -> None, [];;
let getFirstCard (hand:Card*Card) = function
| card1, card2 -> card1
let hand, deckRemaining = deal (shuffle deck);;
let someCard = getFirstCard hand