concept pattern in category erlang

appears as: patterns, pattern, pattern, The pattern, patterns, The patterns
Erlang and OTP in Action

This is an excerpt from Manning's book Erlang and OTP in Action.

  • Upgradability— The framework provides patterns for handling code upgrades in a systematic way.
  • In the previous section, we called = a match operator. This is because what it does is pattern matching, rather than assignment. On the left side, you have a pattern; and on the right side, you have a plain old expression. To evaluate the match, the right-side expression is evaluated first, to get a value. That value is then matched against the pattern (a bit like when you match a string against a regular expression). If the pattern doesn’t match, as in 17 = 42, or true = false, the match fails and throws an exception containing the reason code badmatch. In the shell, this is presented to you as the error message “no match of right hand side value ....”

    If the match works, the program continues with any expressions that follow after it, but now any variables that occurred in the pattern on the left side have the same values as the corresponding parts of the value from the right side. (If the pattern is only a single variable, as in X = 42, then the corresponding part is the entire right-side value.) To illustrate, try the following in the shell:

    1> {A, B, C} = {1970, "Richard", male}.
    {1970,"Richard",male}
    2> A.
    1970
    3> B.
    "Richard"
    4> C.
    male
    5>

    It shouldn’t be hard to see what is happening here. The pattern {A,B,C} matches the right-side tuple; as a result, the variables are bound to the corresponding elements, so you can refer to them afterward. It couldn’t be any simpler.

    Something that consists of an outermost list cell, whose inner layers I don’t care about for now (the | _ ] part of the pattern), but whose payload (in the [ ... | part of the pattern) has the shape {person, [..., ..., ...]}—that is, is a 2-tuple tagged as person, whose second element is a list of exactly three elements. The first of these elements is a 3-tuple labeled name, and I want the third element of that—let’s call it Surname; the third is a 2-tuple labeled tags, and I want the second element of that one—let’s call it Tags.

    Congratulations if you got through all that. But it does show that what a pattern can express in a single line and less than 50 characters can be a rather long-winded business if you spell it out (which you need to do to get the same effect in most other languages). Patterns are natural, compact, readable, and powerful.

    This should give the string "//www.erlang.org" in Rest. This is nice but can be nicer. We said before that operators aren’t allowed in patterns. There is one exception: the ++ operator, which is used to append strings. It can be used in patterns if and only if its left argument is a constant string. The example can then be written as

    "http://" ++ Rest = "http://www.erlang.org"

    (To make things more interesting, we included the two slashes in the pattern as well, giving "www.erlang.org" in Rest.) There is no magic behind this; if you say "abc" ++ SomeString, the ++ operator creates a list of the form [$a, $b, $c | SomeString], like the pattern you wrote by hand previously. Obviously, for the compiler to be able to do this expansion, the left argument of ++ must be a constant string at compile time.