Scott Nimrod
Scott Nimrod

Reputation: 11595

Unable to execute function

I am just not getting F#.

Specifically, I am unable to step into the following code:

let count = hand |> getHandCount

It's like that entire line is just being ignored in the debugger.

Here's the test:

[<Test>]
let ``get card count`` () =

    let hand, deckRemaining = deal (shuffle deck)
    let count = hand |> getHandCount

    count |> should be (greaterThan 0)

Here's the 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}

type Deal = | Hand of Card * Card
            | Hit of Card

let getCount (hand:Card list) =
    let getFaceValue = function
        | Two -> 2
        | Three -> 3
        | Four -> 4
        | Five -> 5
        | Six -> 6
        | Seven -> 7
        | Eight -> 8
        | Nine -> 9
        | Ten -> 10
        | Jack -> 10
        | Queen -> 10
        | King -> 10
        | Ace -> 11

    hand |> List.sumBy (fun c -> getFaceValue c.Face)

let getHandCount hand = function
    | Some(card1, card2) -> [card1; card2] |> getCount
    | None -> 0

What important lesson am I missing now?

Upvotes: 0

Views: 65

Answers (2)

TheInnerLight
TheInnerLight

Reputation: 12184

Whenever you write a pattern matching function, e.g.:

let f = function
    |Case1 -> // something
    |Case2 -> // something else

You are writing the equivalent of:

let f = fun arg ->
    match arg with
    |Case1 -> // something
    |Case2 -> // something else

See: https://msdn.microsoft.com/en-us/library/dd233242.aspx

Your getHandCount function therefore takes an argument hand and takes an argument from the implicit lambda which you are not supplying. So just remove the argument hand from your function.

let getHandCount = function
    | Some(card1, card2) -> [card1; card2] |> getCount
    | None -> 0

Upvotes: 1

John Palmer
John Palmer

Reputation: 25516

When you have

let getHandCount hand = function
    | Some(card1, card2) -> [card1; card2] |> getCount
    | None -> 0

if you paste it into the interactive you get

val getHandCount : hand:'a -> _arg1:(Card * Card) option -> int

This tells you that the hand argument is essentially ignored and gethandCount returns a function

realistically I am surprised the code even compiles

Upvotes: 3

Related Questions