Add multiple cons pattern example to F# pattern matching documentation #49073
+17
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #[issue-number]
Summary
This PR adds documentation for chaining multiple cons patterns together in F# pattern matching, addressing a gap in the existing documentation.
Changes
Added a new example demonstrating how to use multiple cons patterns consecutively to match lists that start with specific sequences of elements. This is a useful pattern that was previously undocumented.
Before: The documentation only showed the basic cons pattern (
head :: tail
) for decomposing a list into its first element and remaining elements.After: The documentation now includes an example showing how to chain cons patterns:
This pattern is particularly useful when you need to match lists based on their first few elements, providing a more elegant alternative to using list patterns with fixed lengths.
Files Changed
docs/fsharp/language-reference/pattern-matching.md
- Added explanatory text and reference to new code snippetsamples/snippets/fsharp/lang-ref-2/snippet4819.fs
- New runnable example demonstrating multiple cons patternsThe example code has been tested and verified to compile and run correctly with F# Interactive.
Original prompt
Fixes #28742
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.
Internal previews