Basics 10 - Pattern Matching (Examples - Part 1)

Here are some examples of pattern matching.

Learning-FSharp/Ch10-PatternMatchingExamples1/Program.fs

These examples don't cover everything about pattern matching, however, are good enough to get you started, and are required for topics covered in the next few chapters.

Matching for Constant Values

How to match for constants and the wildcard case.

// Number matching with constant values
// If you need to use symbols/names instead of constants like 1, 2
// use Literal attribute
// [<Literal>]
// let One = 1
let constantNumMatching num =
    match num with
    | 1 -> "One"
    | 2 -> "Two"
    | _ -> "No Idea"

// String matching with constant values
let constantStringMatching str =
    match str with
    | "1" -> "One"
    | "2" -> "Two"
    | _ -> "No Idea"

Matching for Discriminated Unions

How to match for discriminated unions including conditions.

// Discriminated union for int or string or nothing/empty
type IntOrString =
    | IntVal of int
    | StrVal of string
    | NoValue

// Discriminated union matching for cases, IntOrString
let intOrStringMatching1 iOrS =
    match iOrS with
    | IntVal(i) -> $"Found integer value {i}"
    | StrVal(s) -> $"Found string value {s}"
    | NoValue -> "Found nothing!"

// Discriminated union matching for cases, IntOrString
// with condition
let intOrStringMatching2 iOrS =
    match iOrS with
    | IntVal(i) when i = 10 -> $"Found integer value 10."
    | IntVal(i) -> $"Found integer value {i}."
    | StrVal(s) -> $"Found string value {s}"
    | NoValue -> "Found nothing!"

// Discriminated union type Visitor
type Visitor =
    | Employee of name: string * employeeId: string
    | NonEmployee of name: string

// Discriminated union matching for cases, Visitor
let visitorMatching vis =
    match vis with
    | Employee(name, employeeId) -> $"An employee visited. Name: {name}, Id: {employeeId}."
    | NonEmployee(name) -> $"A non-employee visited. Name: {name}."

Matching for Tuples

How to match for tuples including conditions.

// Tuple matching / desonctruction
let tupleMatching1 tup =
    match tup with
    | (x, y) -> $"Tuple of two values: {x} and {y}."

// Tuple matching with constant value
let tupleMatching2 tup =
    match tup with
    | (0, y) -> $"First value: 0, second: {y}."
    | (x, 0) -> $"First value {x}, second: 0."
    | _ -> $"Seems both values are non-zero."

// Tuple matching with conditions
let tupleMatching3 tup =
    match tup with
    | (x, y) when x > y -> $"First value ({x}) is > the second ({y})."
    | (x, y) when y > x -> $"Second value ({y}) is > the first ({x})."
    | _ -> $"Seems both values are the same."

Matching for Records

How to match for records including conditions.

// Record type FullName
type FullName = { First: string; Second: string }

// Record matching, FullName
let recordMatching1 fullName =
    match fullName with
    | { First = first; Second = second } -> $"Full name is {first} {second}."

// Record matching with condition
let recordMatching2 fullName searchFirstName =
    match fullName with
    | { First = first; Second = second } when first = searchFirstName ->
        $"{searchFirstName} is a match Full name is {first} {second}."
    | _ -> "First name isn't {searchFirstName}."

If you have reached so far, congratulations.

Keep reading!