Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/fsharp/language-reference/pattern-matching.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions samples/snippets/fsharp/lang-ref-2/snippet4819.fs
Original file line number Diff line number Diff line change
@@ -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']
Loading