September 2013
Intermediate to advanced
548 pages
12h 25m
English
Let’s suppose we have some code like this:
| Line 1 | func1([{tag1, A, B}|T]) -> |
| 2 | ... |
| 3 | ... f(..., {tag1, A, B}, ...) |
| 4 | ... |
In line 1, we pattern match the term
{tag1, A, B}, and in line 3, we
call f with an argument that is {tag1, A,
B}. When we do this, the system rebuilds the term {tag1,
A, B}. A much more efficient and less error-prone way to do
this is to assign the pattern to a temporary variable, Z,
and pass this into f, like this:
| | func1([{tag1, A, B}=Z|T]) -> |
| | ... |
| | ... f(... Z, ...) |
| | ... |
The match operator can be used at any point in the pattern, so if we have two terms that need rebuilding, such as in this code:
| | func1([{tag, {one, A}, B}|T]) -> |
| | ... |
| | ... f(..., {tag, {one,A}, B}, ...), |
| | ... g(..., ... |
Read now
Unlock full access