WolframLang: Pattern Matching
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-side → right-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:
- Local transformation: Functions that take a expression and list of rules, and return a new expression.
- Global transformation: A set a rules defined globally. Evaluation of any expression uses these rules to transform expression.
Creating Rules
- For local transformation: WolframLang: Rule, RuleDelayed
- For global transformation: WolframLang: Set, SetDelayed
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.