Pattern matching
Pattern matching is the process of comparing values or input with specified patterns to identify matches, classify data, or select corresponding behavior.
Functional Programming in Scala, Second Edition
Pattern matching is a language feature that selects a case by comparing a target expression with structural patterns and evaluates the corresponding result.
- How it is used
- Uses pattern matching to define pure operations over immutable functional data structures.
- How deep it goes
- Explains case order, nested destructuring, bindings, wildcards, guards, and exhaustiveness through recursive list functions.
- Examples
- Works through `sum`, `product`, nested `Cons` matching, `tail`, `dropWhile`, and pairwise list operations.
- What you can do after
- Apply it via Scala match expressions to destructure lists and write recursive pure functions.
Read how Functional Programming in Scala, Second Edition explains pattern matching →
Natural Language Processing in Action, Second Edition
A pattern-matching algorithm selects behavior or responses by comparing an input with predefined textual patterns.
- How it is used
- Uses predefined patterns to recognize chatbot input and extract information from natural-language strings.
- How deep it goes
- Works through greeting expressions and capture groups, then applies regular expressions to sentence segmentation.
- Examples
- Demonstrates greeting and name matches, and splits sentences using punctuation patterns.
- What you can do after
- Apply pattern matching via Python's re module to recognize greetings and inspect captured groups.
Read how Natural Language Processing in Action, Second Edition explains pattern matching →
Elixir in Action, Third Edition
Pattern matching is a language mechanism that compares a value with a pattern and succeeds when their structure and contents conform, binding variables in the pattern.
- How it is used
- Uses structural data checks to decompose values, select function clauses, validate results, and express recursive iteration.
- How deep it goes
- Explains tuple, list, binary, nested, chained, constant, function-argument, and with-pattern behavior, while deferring the complete pattern reference.
- Examples
- Demonstrates people tuples, calendar values, file reads, binary commands, geometry dispatch, user extraction, and recursive list processing.
- What you can do after
- Apply it to destructure data, dispatch functions by input shape, compose tagged operations, and implement recursive list processing.
Read how Elixir in Action, Third Edition explains pattern matching →
F# in Action: A Revised Edition of Get Programming with F#
Pattern matching is a language feature that selects behavior by testing a value against structural or value patterns and binding matched components.
- How it is used
- Uses pattern matching as F#’s main conditional expression for domain decisions and consuming discriminated unions.
- How deep it goes
- Works through match syntax, ordered and exhaustive branches, and tuple, record, list, and union patterns; defers other pattern types to documentation.
- Examples
- Uses loan-eligibility rules, contact methods, loan-request lists, optional numbers, and instrument sounds to show different matches.
- What you can do after
- Practice/perform F# matches that simplify conditional rules, safely extract case data, and use compiler warnings to find omissions.
Read how F# in Action: A Revised Edition of Get Programming with F# explains pattern matching →
The Art of Code
Pattern matching is a language mechanism that tests an object's structure and can extract its components when the structure matches.
- How it is used
- Uses pattern matching to route behavior based on data structure and make branching intent clear.
- How deep it goes
- Defines targets, predicates, and bindings, then demonstrates guarded switches and record component extraction.
- Examples
- Uses BMI classification by age and a Number-to-int refactoring exercise.
- What you can do after
- Apply it via Java pattern matching to route data and refactor type-based conversions.
Learn Haskell by Example
Pattern matching is a language mechanism that selects computation branches by comparing values against structural patterns.
- How it is used
- Structures recursive list functions and command-line argument parsing around the possible shapes of input.
- How deep it goes
- Explains list constructors, recursive equations, exhaustiveness, single-element and wildcard patterns; defers error values to Maybe.
- Examples
- Demonstrates listLength, indexOf, a Caesar cipher, and parsing a single filename argument.
- What you can do after
- Build recursive Haskell functions that deconstruct lists, cover structural cases, and discard unused fallback input.
Read how Learn Haskell by Example explains pattern matching →
Type-Driven Development with Idris
A programming technique that defines behavior by distinguishing values according to their structure or constructor.
- How it is used
- Defines behavior by input structure and refines state information needed to satisfy operation preconditions.
- How deep it goes
- Works through case splitting, hole inspection, recursive definitions, type refinement, and totality checks.
- Examples
- Uses Boolean inversion, list descriptions, word-length vectors, natural-number addition, and a vending machine.
- What you can do after
- Builds pattern-matching functions from type signatures and uses holes and totality checks to refine them.
Read how Type-Driven Development with Idris explains pattern matching →
Idiomatic Rust
Pattern matching is a language feature that selects behavior by testing a value against structurally defined patterns and optionally binding parts of the value.
- How it is used
- Controls runtime code flow by matching values, destructuring data, and handling optional cases.
- How deep it goes
- Explains exhaustive matches, bindings, guards, and value and structure patterns through Rust examples and compiler diagnostics.
- Examples
- Uses Option cases, integer ranges, tuple destructuring, enum variants, struct fields, and guarded matches.
- What you can do after
- Apply exhaustive matches to unwrap Option values, destructure data, select behavior, and handle cases clearly.
Functional Programming in C#, Second Edition
Pattern matching is a language feature that tests a value against patterns describing its value, type, or structure.
- How it is used
- Uses switch-based matching to select VAT rules from country, product, and address data.
- How deep it goes
- Demonstrates value, record, property, and type patterns through VAT examples.
- Examples
- Uses country VAT rates, Germany’s food distinction, and US state rates as examples.
- What you can do after
- Apply C# switch expressions to select VAT calculations by country, record structure, or address type.
Read how Functional Programming in C#, Second Edition explains pattern matching →
DuckDB in Action
Pattern matching is the comparison of strings against specified wildcard or regular-expression patterns.
- How it is used
- Filters relational data by wildcard, filename-like, or regular-expression conditions.
- How deep it goes
- Defines four matching approaches and demonstrates one regular-expression filter, but does not develop regex syntax.
- Examples
- Uses a country population table to locate Eastern Europe records and intersect them with a population threshold.
- What you can do after
- Apply it via DuckDB to filter strings and combine matching records with other relational results.
Learn Rust in a Month of Lunches
Pattern matching is the process of comparing a value against structural patterns and handling the matching case.
- How it is used
- Uses structural cases to replace crowded conditional chains and safely handle alternatives such as Option values.
- How deep it goes
- Explains arm syntax, exhaustive coverage, wildcards, expressions, tuples, guards, ordering, and compatible result types.
- Examples
- Uses numbers, weather tuples, color tuples, marital status, and Some or None values as matching examples.
- What you can do after
- Apply it via Rust match expressions to branch on values, extract nested data, return results, and handle missing cases.
Read how Learn Rust in a Month of Lunches explains pattern matching →
Functional Design and Architecture: Examples in Haskell
Pattern matching is a language mechanism that selects behavior by matching a value against structural patterns and binding its components.
- How it is used
- Uses structural cases to calculate results, propagate optional failures, and interpret free-monadic domain scripts.
- How deep it goes
- Explains extraction, typed rejection, nested Maybe cases, and recursive Free interpretation, but defers formal matching semantics.
- Examples
- Demonstrates Shape area calculation, nested Maybe lookups, do notation, and a SandwichRecipe free interpreter.
- What you can do after
- Apply it via Haskell definitions to branch on data constructors, extract fields, and process recursive interpreter structures.
Read how Functional Design and Architecture: Examples in Haskell explains pattern matching →
The Well-Grounded Rubyist, Fourth Edition
Pattern matching is a technique for selecting behavior by testing the structural shape and contents of a value against declarative patterns.
- How it is used
- Uses regular expressions for text processing and case...in for branching on object structure.
- How deep it goes
- Explains regular-expression applications, structural captures, type constraints, guards, and grep's reliance on ===.
- Examples
- Demonstrates array-shape descriptions, grade calculation, regular-expression selection, class selection, and range selection.
- What you can do after
- Apply pattern matching through Ruby regular expressions, case...in statements, and Enumerable#grep.
Read how The Well-Grounded Rubyist, Fourth Edition explains pattern matching →
Understanding AI
Pattern matching is the computational technique of identifying input structures that correspond to predefined or learned patterns.
- How it is used
- Explains why systems can produce convincing language without understanding, supporting more careful use of AI output.
- How deep it goes
- Defines keyword detection, structural matching, and template transformation, then contrasts rules with learned patterns without explaining model internals.
- Examples
- Uses ELIZA’s keyword rules, “I feel X” templates, therapeutic questions, and the contrast between ELIZA and modern AI.
- What you can do after
- Evaluate/understand when fluent AI responses may simulate meaning rather than demonstrate understanding.
100 C++ Mistakes and How to Avoid Them
Pattern matching is the process of determining whether structured data conforms to a specified pattern.
- How it is used
- Uses pattern matching to validate or break apart textual input when hand-written checks become difficult to maintain.
- How deep it goes
- Works through email validation, contrasting character checks with regular expressions and noting custom parsers’ precision and maintenance risks.
- Examples
- Contrasts hand-coded email checks with a std::regex-based email-validation example.
- What you can do after
- Apply it via C++ std::regex and regex_search to check textual input such as an email address.
Read how 100 C++ Mistakes and How to Avoid Them explains pattern matching →
Machines that Think
Pattern matching is the identification or reproduction of regularities in data without necessarily representing their underlying meaning.
- How it is used
- Explains how rule-based systems imitate conversational behavior without establishing that they understand meaning.
- How deep it goes
- Defines pattern matching through scripted language behavior and contrasts it with contextual interpretation, without detailing implementation.
- Examples
- Uses ELIZA’s scripted replies and the ambiguous word “bank” in translation to illustrate surface matching’s limits.
- What you can do after
- Evaluate fluent responses as evidence of surface pattern reproduction rather than proof of contextual understanding.
Also covered in
- Grokking Web Application Security
- Rust in Action: Systems programming concepts and techniques
- Learn SQL in a Month of Lunches
- Pro ASP.NET Core 7, Tenth Edition
- Rust Web Development
- The Quick Python Book, Fourth Edition
- Data-Oriented Programming in JavaIn early access (MEAP)
- Event Processing in Action
What to know first
- genericsGenerics are programming-language constructs parameterized by types, allowing definitions to apply to multiple concrete types while preserving type relationships.
- listA list is an ordered collection of elements arranged in a sequence.
- switch statementA switch statement is a control-flow construct that selects an execution branch by matching an expression's value against alternative cases.
- functional programmingFunctional programming is a programming paradigm that expresses computation through the evaluation and composition of functions, typically favoring immutable data and minimizing mutable state and side effects.
Related concepts
- constructor methodused by · A constructor method is a type-associated operation involved in creating an instance and establishing its initial state.
- assignmentcontrasts with · Assignment is a programming-language operation that binds or stores an evaluated value in a variable, property, or other assignable location.
- rule-based systemused by · A rule-based system is a computational system whose behavior or outputs are determined by applying explicitly specified rules to inputs, facts, or conditions.
Questions about pattern matching
What is pattern matching?
Pattern matching is the process of comparing values or input with specified patterns to identify matches, classify data, or select corresponding behavior.
Which Manning books explain pattern matching?
Functional Programming in Scala, Second Edition, Natural Language Processing in Action, Second Edition, Elixir in Action, Third Edition, F# in Action: A Revised Edition of Get Programming with F#, The Art of Code, Learn Haskell by Example, Type-Driven Development with Idris, Idiomatic Rust and 8 more.
What should I know before learning pattern matching?
Manning books that teach pattern matching usually introduce generics, list, switch statement and functional programming first.
What is the difference between pattern matching and assignment?
Pattern matching is the process of comparing values or input with specified patterns to identify matches, classify data, or select corresponding behavior. Assignment is a programming-language operation that binds or stores an evaluated value in a variable, property, or other assignable location.
What kinds of pattern matching are there?
Kinds of pattern matching include extended pattern matching.
What are the parts of pattern matching?
Pattern matching includes term.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.















