4.1 Pattern Matching

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

In a 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, a run-time error occurs.

Examples:

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