In this chapter, a number of miscellaneous features of Erlang are described.
In Erlang 4.8 (OTP R5A) the syntax of Erlang tokens have been extended to allow the use of the full ISO-8859-1 (Latin-1) character set. This is noticeable in the following ways:
The new characters from Latin-1 have the following classifications in Erlang:
Two adjacent string literals are concatenated into one. This is done already at compile-time, and doesn't incur any runtime overhead. Example:
"string" "42"
is equivalent to
"string42"
This feature is convenient in at least two situations:
Since list concatenation is a very common operation, it is convenient to have a terse way of expressing it. The ++ operator appends its second argument to its first. Example:
X = [1,2,3],
Y = [4,5],
X ++ Y.
results in
The ++ operator has precedence between the binary '+' operator and the comparison operators.
The - - operator produces a list which is a copy of the first argument, subjected to the following procedure: for each element in the second argument, its first occurrence in the first argument is removed.
X = [1,2,3,2,1,2],
Y = [2,1,2],
X -- Y.
results in
The - - operator has precedence between the binary '+' operator and the comparison operators.
Apart from the binary bitwise operators
bnot 7.
returns -8.
The atoms
M1 = lists:member(A, List1),
M2 = lists:member(A, List2),
M1 and M2.
Note that the operators are strict, i.e., they always evaluate both their arguments.
This extension was added in Erlang 4.8 (OTP R5A).
The = operator is also called the `match' operator. The match operator
can now be used in a pattern, so that
One use for this construction is to avoid reconstructing a term which was part of an argument to a function. Example:
f({'+',X,Y}=T) -> {X+Y,T}.
It also makes it possible to rewrite the construction
f(X) when X == #rec{x=1, y=a} -> ...
as
f(#rec{x=1, y=a} = X) -> ...
In the absence of optimization for the former case, the latter case is more efficient.
This extension was added in Erlang 4.8 (OTP R5A).
A new construction is allowed in patterns, namely a literal string as the first operand of the ++ operator. Example:
f("prefix" ++ L) -> ...
This is syntactic sugar for the equivalent, but harder to read
f([$p,$r,$e,$f,$i,$x | L]) -> ...
This extension was added in Erlang 4.9 (OTP R6A).
A new construction is allowed in guards, the disjunction operator ';'. The construction is syntactic sugar which removes the bother of writing the same body after several guards.
f(X) when xxx ; yyy ; zzz ->
pop(X).
This is syntactic sugar for the equivalent
f(X) when xxx ->
pop(X);
f(X) when yyy ->
pop(X);
f(X) when zzz ->
pop(X).
The abstract format has been changed accordingly to contain a list of (conjunctive) guards where there was previously only one guard.
This extension was added in Erlang 5.0 (OTP R7A).
An arithmetic expression can be used within a pattern, if it uses only numeric or bitwise operators, and if its value can be evaluated to a constant at compile-time. This is especially useful when the expression is defined by a macro.
Example:
case X of
{1+2, T} -> T
end.
This extension was added in Erlang 5.1 (OTP R8).
In guards, the use of
The guard expressions written with these operators are boolean
expressions, and the boolean functions
Example 1:
f(X) when not (is_tuple(X) or is_list(X)) ->
...
Example 2:
0) and (B > 0) and not (A*A < B*B) ->
... ]]>
This extension was added in Erlang 5.1 (OTP R8).
In a boolean expression it is unnecessary to always evaluate all terms. If the first term gives a result that determines the result, the second term is not needed. In Erlang two new keywords handles boolean expressions without evaluating both terms, if it's unnecessary. (This is called short-curcuit boolean evaluation.)
The keyword
Example 1:
case A >= -1.0 andalso math:sqrt(A+1) > B of
This will work even if
Example 2:
OnlyOne = is_atom(L) orelse
(is_list(L) andalso length(L) == 1),