Erlang (programming language)/Tutorials/Pattern Matching

From Citizendium
Jump to navigation Jump to search

Erlang uses powerful pattern-matching to bind variables to values. Pattern matching can be explicit, as when the = (pattern matching operator) is used, or implicit, as when a function call is processed and a function's actual arguments are matched with its formal parameters.

Patterns

Patterns look the same as terms (they can be simple literals like atoms and numbers, compound like tuples and lists, or a mixture of both. They can also contain variables, which are alphanumeric strings that begin with a capital letter or underscore. A special "anonymous variable", _ (the underscore) is used when you don't care about the value to be matched, and won't be using it.

Pattern matching vs unification

Please note the difference between pattern matching and unification. Erlang uses pattern matching, while prolog uses unification. Pattern matching is strictly one way, from right to left. Fully qualified values must exist on the right of any erlang match. Whereas, unification matches variables to values in both directions. Unification is much more complicated for both humans and machines to do.

A pattern matches if it has the same "shape" or "data structure" as the term being matched, and atoms encountered are the same. For example, the following matches succeed: [1]

  • A = 1.
  • 1 = 1.
  • {ok, A} = {ok, 42}.
  • [H|T] = [1, 2, 3].
  • Msg = {server, client, {hello,[one,two,three],goodbye}}.
  • {From,To,Body} = Msg.
  • {Header, SomeData, _Epilog} = Body.

Note:

 H=1 and T=[2,3]
 From=server, To=client, and Body={hello,[one,two,three],goodbye}
 Header=hello, SomeData=[one,two,three], and _Epilog=goodbye but it is ignored. 

The nice thing about pattern matching is complexity can be hidden until it is needed. Each variable can match large complicated nests of tuples and lists like the Body variable. Also pattern matching can be very efficient and easy on the eyes to tag various parts to be ignored with the underscore prefix. Note that in the fourth example, the pipe (|) signifying the break between the head and tail of the list as described in Terms.

These matches fail:

  • 1 = 2.
  • {ok, A} = {failure, "Don't know the question"}.
  • [H|T] = [].

In the case of the pattern-matching operator, a failure generates an error and the process exits. How this can be trapped and handled is covered in Errors.

Patterns are used to select which clause of a function will be executed (this is covered in Functions; which option to select in a case expression (Expressions); and which messages to retrieve from the mailbox (Processes).

Variables

Erlang variables are single-assignment variables that do not have to be declared. They are written with a capital letter or underscore followed by an alphanumeric sequence. They are bound to values using the pattern matching mechanism. The Erlang compiler will produce an error if an unbound variable is used, and a warning if a bound variable is not used. Sometimes you might encounter a _Var variable. This variable is bound and does contain a value, however it suppresses compiler warnings regarding unused bound variables.

Notes

  1. Since Erlang variables are immutable, consider examples like this to be standalone--even though the A is used twice, it's a different A each time!