活动模式 - 连续元素的检查列表

Active pattern - Check list for consecutive elements

所以作为一项学校作业,我必须编写一个识别扑克手的部分活动模式,例如:

let (|FullHouse|_|) (x: (string * string) list) =
    x
    |> List.groupBy snd
    |> List.sortByDescending fst
    |> List.forall (fun (_, v) -> v |> List.length > 1)
    |> function true -> Some () | _ -> None

let x6 =
    match [("S", "2"); ("S", "2"); ("S", "Q"); ("S", "Q"); ("S", "Q")] with
    | FullHouse -> sprintf "Full house"
    | _ -> "Nothing"

我成功地完成了 9 手牌中的 6 手牌,但我不知道如何检查这手牌是否有顺子(这 5 个值是连续的,例如 3、4、5、 6, 7).有没有清单。哪位接线员可以帮我做这件事?

在评论的帮助下,我设法解决了这个问题,我将解决方案发布在这里,供遇到相同(或类似)问题的其他人使用。

let GiveValue (x: string) =
    match x with
    | "2" -> 2
    | "3" -> 3
    | "4" -> 4
    | "5" -> 5
    | "6" -> 6
    | "7" -> 7
    | "8" -> 8
    | "9" -> 9
    | "10" -> 10
    | "J" -> 11
    | "Q" -> 12
    | "K" -> 13
    | "A" -> 14
    | _ -> 0

let (|Straight|_|) (x: (string * string) list) =
    x
    |> List.map (fun (_, v) -> v)
    |> List.map GiveValue
    |> List.sort
    |> List.pairwise
    |> List.forall (fun (x, y) -> y-x = 1)
    |> function true -> Some () | _ -> None

let x4 =
    match [("S", "J"); ("D", "9"); ("S", "8"); ("S", "10"); ("S", "Q")] with
    | Straight -> sprintf "Straight"
    | _ -> "Nothing"