# Introduction to F#'s pattern matching

 MVP This article is provided by MVP Eriawan Kusumawardhono. Microsoft is so thankful that MVPs who proactively share their professional experience with other users. The article would be posted on MVP's website or blog later.

##### Introductions
F#'s pattern matching is one of the great feature of F# and also a common feature in functional programming languages.
It greatly simplifies complex conditional and branching codes using set of expression patterns.
Pattern matching in F# is available since F#'s initial release.

##### Pattern matching syntax
The pattern matching syntax in F# are available in two kinds: match expressions and within a function(or simply a "Pattern matching function").

The syntaxes are:

// Match expression.
match test-expression with
| pattern1 [ when condition ] -> result-expression1
| pattern2 [ when condition ] -> result-expression2
| ...

// Pattern matching function.
function
| pattern1 [ when condition ] -> result-expression1
| pattern2 [ when condition ] -> result-expression2
| ...

The most common usages are match expressions.

The "|" in pattern matching separates each patterns. First "|" is optional.

For example, we have a function that differentiate odd or even number and will yield result "odd" or "even":

let OddEven a = let b = a % 2

match b with
| 0 -> "even"
| _ -> "odd"

The explanations for the above code is:

As illiustrated, the "odd" and "even" are function results from patterns. Because of mod 2 is only 0 and 1, therefore the result only match 0 and 1. But F# compiler will also consider other values as result pattern, we can safely conclude 0 is even and the rest (including 1) is odd. The pattern to use is simply using "_" as wildcard.

The patterns used are Constant pattern and Wildcard pattern (the "_").

##### Patterns in F# pattern matching
These are the patterns in F#'s pattern matching:
• Constant pattern
• Identifier pattern
• Wildcard pattern
• List pattern
• Array pattern
• Variable pattern
• as pattern
• OR pattern
• AND pattern
• Cons pattern
• Parenthesized pattern
• Tuple pattern
• Record pattern
• Pattern with type annotation
• Type test pattern
The most common usages are constant, wildcard, cons and list patterns.

Common sample of cons are heads and tails in collection, such as this sample to simply concatenate a collection of string:

let rec conactStringList =
| [] -> ""
As described in MSDN Library, the cons pattern is used to decompose a list into the first element, the head, and a list that contains the remaining elements, the tail. Again, please pay attention to the indentation. The sample above is using function pattern matching, instead of using match.

The syntax of word head and tail can be replaced by other word but it's advisable to use "head" and "tail" for simplicity because it's easier to understand.

##### Using guard on patterns
Sometimes we need to have more condition to evaluate further. We can't use values other than literals, especially to express multiple conditions. This additional variable is called guard. We can use "guard" to have multiple conditions.
You can use a when clause to specify an additional condition that the variable must satisfy to match a pattern. The expression following the when keyword is not evaluated unless a match is made to the pattern associated with that guard.

The following example from MSDN Library illustrates the use of a guard to specify a numeric range for a variable pattern. Note that multiple conditions are combined by using Boolean operators.

The guard in F# is simply other symbol with conditions to be evaluated. For example:

let rangeTest testValue mid size =
match testValue with
| var1 when var1 >= mid - size/2 && var1 <= mid + size/2 -> printfn "The test value is in range."
| _ -> printfn "The test value is out of range."

Then test the output:

rangeTest 10 20 5
rangeTest 10 20 10
rangeTest 10 20 40

when in the sample above is treated as keyword in F#:

F# IDE will differentiate when as keyword (colored blue).