diff --git a/docs/fsharp/language-reference/pattern-matching.md b/docs/fsharp/language-reference/pattern-matching.md index b9d23162ffe36..83ccd0bff162d 100644 --- a/docs/fsharp/language-reference/pattern-matching.md +++ b/docs/fsharp/language-reference/pattern-matching.md @@ -138,6 +138,10 @@ The cons pattern is used to decompose a list into the first element, the *head*, [!code-fsharp[Main](~/samples/snippets/fsharp/lang-ref-2/snippet4809.fs)] +You can also chain multiple cons patterns together to match lists that start with specific sequences of elements. + +[!code-fsharp[Main](~/samples/snippets/fsharp/lang-ref-2/snippet4819.fs)] + ## List Pattern The list pattern enables lists to be decomposed into a number of elements. The list pattern itself can match only lists of a specific number of elements. diff --git a/samples/snippets/fsharp/lang-ref-2/snippet4819.fs b/samples/snippets/fsharp/lang-ref-2/snippet4819.fs new file mode 100644 index 0000000000000..9ddeeadde731d --- /dev/null +++ b/samples/snippets/fsharp/lang-ref-2/snippet4819.fs @@ -0,0 +1,13 @@ +let charList = ['A'; 'B'; 'C'; 'D'] + +// This example demonstrates multiple cons patterns. +let matchChars xs = + match xs with + | 'A'::'B'::t -> printfn "starts with 'AB', rest: %A" t + | 'A'::t -> printfn "starts with 'A', rest: %A" t + | 'C'::'D'::t -> printfn "starts with 'CD', rest: %A" t + | _ -> printfn "does not match" + +matchChars charList +matchChars ['A'; 'X'] +matchChars ['C'; 'D'; 'E']