Pattern Guard
Noun · Development
Definitions
Pattern Guard is a control flow construct found in functional programming languages that adds boolean conditions to pattern matching clauses. While a basic pattern match checks the structure and shape of data, a pattern guard allows additional arbitrary conditions to be evaluated when a pattern matches. For example, in Haskell, a function matching on a number can include a guard like | x > 0 to only match positive values. Rust uses if conditions after match arms for the same purpose. Pattern guards make pattern matching more expressive without requiring nested if-else statements inside match clauses. They are commonly used to filter matches based on computed properties, compare matched values against other variables, or add range constraints. The combination of pattern matching and guards creates a concise, declarative way to handle complex branching logic that would otherwise require verbose conditional chains. Erlang, Elixir, Scala, and F# all support forms of pattern guards.
In plain English: An extra condition added to pattern matching that says 'match this pattern, but only if this other thing is also true.'
Example: "Match on the HTTP status code with a pattern guard: 200..=299 if body.is_some() => process the response."
Etymology
- 1977
- Pattern matching appears in ML (the language), with guards providing conditional refinement of matched patterns.
- 1990
- Haskell formalizes pattern guards, allowing boolean conditions to augment pattern matching in function definitions.
- 2010s
- Rust, Scala, and Swift adopt pattern guards, bringing the concept to mainstream systems and application programming.
- 2020s
- Pattern matching with guards appears in Python (match/case, PEP 634) and evolves in other languages, gaining broader adoption.
Origin Story
The Bouncer That Checks Your Pattern's ID
A pattern guard is a boolean expression attached to a pattern match that adds an additional condition beyond structural matching. If the pattern matches but the guard evaluates to false, the match is rejected and the next pattern is tried. The concept originated in functional programming languages. Miranda, developed by David Turner at the University of Kent in 1985, was among the first languages to support guards in pattern matching. Haskell, which emerged from a 1987 committee effort to create a standard lazy functional language, adopted and popularized guards as a core feature. In Haskell, guards appear after a pattern separated by a pipe character, allowing programmers to express complex dispatch logic concisely. The idea spread beyond functional languages. Scala, Rust, Swift, and Elixir all support some form of pattern guards, though the syntax varies. In Rust, the guard appears as an 'if' clause after the pattern in a match arm. Pattern guards are particularly useful when the condition depends on computed values or relationships between variables that cannot be expressed through structural matching alone. For example, matching a pair of numbers where the first must be greater than the second requires a guard, since the structural pattern alone cannot express ordering constraints.
Context: Miranda (1985) and Haskell (1987) functional programming languages
Fun fact: Haskell's pattern guards were named after the mathematical concept of guarded equations, where conditions determine which equation applies. The syntax is designed to read like mathematical notation.