无法执行功能

时间:2015-12-28 22:34:15

标签: f#

我只是没有得到F#。

具体来说,我无法进入以下代码:

let count = hand |> getHandCount

就像整个行在调试器中被忽略一样。

以下是测试:

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

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

    count |> should be (greaterThan 0)

以下是代码:

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

我现在缺少什么重要的一课?

2 个答案:

答案 0 :(得分:3)

当你有

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

如果您将其粘贴到您获得的互动中

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

这告诉您hand参数基本上被忽略,gethandCount返回一个函数

实际上我很惊讶代码甚至编译

答案 1 :(得分:1)

每当你编写模式匹配函数时,例如:

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

你写的相当于:

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

请参阅:https://msdn.microsoft.com/en-us/library/dd233242.aspx

因此,getHandCount函数接受参数hand ,而从您未提供的隐式lambda中获取参数。所以只需从函数中删除参数hand

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