Pattern Matching — Erlang System Documentation v29.0.1

Search documentation of Erlang System Documentation

# Pattern Matching

Copy Markdown View Source

## Pattern Matching

Variables are bound to values through the pattern matching mechanism. Pattern matching occurs when evaluating the `case`, `receive`, `try`, and the match operator (`=`) expressions.

In pattern matching, a left-hand side pattern is matched against a right-hand side term. If the matching succeeds, any unbound variables in the pattern become bound. If the matching fails, an exception is raised.

Examples:

    1> X.
    ** 1:1: variable 'X' is unbound **
    2> X = 2.
    2
    3> X + 1.
    3
    4> {X, Y} = {1, 2}.
    ** exception error: no match of right hand side value {1,2}
    5> {X, Y} = {2, 3}.
    {2,3}
    6> Y.
    3

← Previous Page Data Types
Next Page → Modules