WolframLang: Pattern Matching

By Xah Lee. Date: . Last updated: .

What is Pattern Matching

Pattern Matching is a core mechanism how WolframLang does computation. WolframLang is a term-rewriting system, meaning, it transform expressions according to rules until no rule matches.

What is Transformation Rules

A rule is a pair of values that looks like this:

left-hand-sideright-hand-side

The left-hand-side is a pattern (which includes literal expression).

The right-hand-side is replacement, which may include parts of the captured pattern.

Local vs Global Transformation Rules

For the transformation, there are local and global transformations:

Creating Rules

Pattern Syntax

Functions that Transform Expression by Pattern

All Other Functions that Use Patterns

Pattern is not just used for transformation of expression. It can be also used to extract elements in a list, check if a item exist, or as compact conditionals.

Pattern Matching