Lesson 5 of 15

Pattern Matching

Pattern Matching

F#'s match expression is more powerful than a switch. It returns a value and handles multiple patterns cleanly:

let n = 3
let name = match n with
           | 1 -> "one"
           | 2 -> "two"
           | 3 -> "three"
           | _ -> "other"  // wildcard: matches anything
printfn $"{name}"  // three

OR Patterns

Use | to match multiple values in one case:

let isWeekend day = match day with
                    | 6 | 7 -> true
                    | _     -> false

Your Task

Write a function season that maps a month number (1–12) to its season:

  • "winter" for months 12, 1, 2
  • "spring" for months 3, 4, 5
  • "summer" for months 6, 7, 8
  • "autumn" for months 9, 10, 11
JS Transpiler loading...
Loading...
Click "Run" to execute your code.