F# Pattern Matching
F# Pattern Matching allows you to match the structure of data with patterns and execute code based on the match. In this article, we will explore F# pattern matching in detail and learn how to use it effectively.
- F# Pattern Matching:
- What is F# Pattern Matching?:
- How to Use F# Pattern Matching?:
- Pattern Matching Of Season By Month:
- Matching on Option type:
- Recursive Fibonacci using Pattern Matching:
- Pattern Matching Functions:
- Adding Filters or Guards to Patterns:
- F# Pattern Match With Tuples:
- Pattern Matching with Lists:
- Pattern Matching with Records:
- F# Pattern Match Benefits:
What is F# Pattern Matching?
F# Pattern matching is a language construct that allows you to match patterns in data structures and perform operations based on the match. It is a powerful feature that makes code more concise and expressive. F# has a sophisticated pattern matching system that provides a wide range of pattern matching capabilities.
The pattern matching feature of F# is one of the most powerful techniques available for decomposing and extracting information from complex data structures, including lists, records, discriminated unions, as well as their functional relationships, in a concise and expressive manner.
How to Use F# Pattern Matching?
In F#, pattern matching is achieved using the match expression. The match expression takes a value and matches it against one or more patterns. If the value matches one of the patterns, the corresponding action is executed. If the value does not match any of the patterns, an exception is thrown.
Thus, it is one of the most convenient and powerful ways to test data against a number of different criteria, which makes it more efficient and flexible. Furthermore, it is also capable of performing some calculations that are based on the conditions being met.
Syntax
The syntax for pattern matching in F# is as follows:
match expr with | pat1 - result1 | pat2 -> result2 | pat3 when expr2 -> result3 | _ -> defaultResult
Where,
- Conditions are defined by the | symbol.
- A -> symbol indicates “return this value if the condition is true…”
- _ provides the default pattern, which matches everything like a wildcard.
Pattern Matching Of Season By Month
Here is an example of how a pattern matching of weather season by month can be achieved:
Example: 
And output will be as:
Winter Spring Rainy Summer Autumn
Matching on Option type
Below is an example of an option type using pattern matching:
Example: 
This is what it prints out as a result:
Value: 22 None Value: 24
Recursive Fibonacci using Pattern Matching
This example prints out the Fibonacci series using the recursive method of pattern matching:
Example: 
And it prints out:
Fibonacci Series: 1, 1, 2, 3, 5, 8, 13, 21,
Pattern Matching Functions
By using the function keyword, you are able to write functions that match patterns using the following syntax:
Example: 
And the output will be:
Pineapple: 22 Watermelon: 19.5 Mango: 25 Orange: 12.22 Apple: 8.15
Adding Filters or Guards to Patterns
By using the when keyword, you will be able to add filters, or guards, to patterns.
Example: 
The result prints out the following:
1 -1 0
Comparing numbers by using the when keyword, which is used as an if condition:
Example: 
And the output will be as follows:
25 is greater than 21 65 is less than 88 3 equals 3
F# Pattern Match With Tuples
The following example shows how tuples can be used to match patterns:
Example: 
Output of the above example will be Name: John Age: 25 City: New York Date of Birth: 18/march/2002 Name: Ben Age: 32 City: San Antonio Date of Birth: 12/august/1998
Pattern Matching with Lists
Pattern matching in F# can also be used with lists.
You can match on the structure of a list, including its length and individual elements.
Here’s an example of pattern matching with a list:
Example: 
In this example, the listLength function takes a list parameter and matches it against four patterns using the match expression. If the list is empty, the function returns 0. If the list has one element, the function returns 1. If the list has two elements, the function returns 2. If the list has more than two elements, the function returns 3.
Pattern Matching with Records
User records using pattern matching technique:
Example: 
Example Explanation
As a group, we have defined a record type called user with fields Name, Age, City, and Dob which represent user information. We then created a function called printUserInfo that takes a user record as input.
When we call the printUserInfo function with user1, we use pattern matching to destructure the user record into its fields Name, Age, City, and Dob using the record pattern { Name = name; Age = age; City = city; Dob = dob; }. We then print out the values of these fields using the printfn function with appropriate formatting.
User1 output would be:
Name: John Age: 25 City: New York Date of Birth: 18/March/2002
Similarly, when we call the printUserInfo function with user2, it prints out the values of the user2 record’s fields in a similar fashion.
User2 would output:
Name: Ben Age: 32 City: San Antonio Date of Birth: 12/August/1998
F# Pattern Match Benefits
Here are some benefits of using F# pattern matching:
- F# Pattern matching allows you to write concise and expressive code that is easy to read and understand. You can match values against patterns, eliminating the need for long if-else or switch statements.
- Using pattern matching in your code improves readability by making the code more self-documenting. With pattern matching, the code expresses the intent of the developer more clearly, making it easier to understand.
- F# Pattern matching can help catch errors at compile-time, reducing the likelihood of runtime errors. By matching values against patterns, you can ensure that the code is only executed when the values meet specific conditions.
- Pattern matching can be more efficient than using if-else or switch statements, as it allows the compiler to generate optimized code. Pattern matching is also less error-prone than using other methods, as it reduces the chance of bugs being introduced.
- Pattern matching can be used with a wide range of data types, including integers, strings, and user-defined types. You can also use pattern matching with complex data structures, such as lists and arrays.
Conclusion
Pattern matching is a powerful feature in F# that enables you to match values against patterns and perform actions based on the result. In F#, pattern matching is achieved using the match expression. You can match on different types of values, including integers, strings, user-defined types, and lists. Pattern matching in F# allows you to write concise and expressive code, making it an essential tool.
If you liked this article and found it informative regarding F# pattern matching, you can leave your feedback by reacting below.