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:
Octal | Decimal | Class | |
200 - 237 | 128 - 159 | Control characters | |
240 - 277 | 160 - 191 | - ¿ | Punctuation characters |
300 - 326 | 192 - 214 | À - Ö | Uppercase letters |
327 | 215 | × | Punctuation character |
330 - 336 | 216 - 222 | Ø - Þ | Uppercase letters |
337 - 366 | 223 - 246 | ß - ö | Lowercase letters |
367 | 247 | ÷ | Punctuation character |
370 - 377 | 248 - 255 | ø - ÿ | Lowercase letters |
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 [1,2,3,4,5]
.
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 [3,1,2]
.
The - - operator has precedence between the binary '+' operator and the comparison operators.
Apart from the binary bitwise operators band
, bor
and bxor
, there is a unary operator bnot
with the same
precedence as the other unary operators + and -, i.e., higher than
the binary operators. Example:
bnot 7.
The atoms true
and false
are usually used for representing
Boolean values. With the binary operators and
, or
and
xor
, and the unary operator not
, Boolean values can be
combined. Example:
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.
not
has the same priority as the other unary operators. The
binary logical operators have precedence between the =
operator
and the comparison operators, the and
operator having higher
precedence than or
and xor
.
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 P1 = P2
is a valid pattern,
where both P1
and P2
are patterns. This compound pattern
when matched against a term causes the term to be matched against both
P1
and P2
.
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 and
, or
and not
is
now allowed. Guard expressions can combine these with
parenthesis. This allows for more elaborate guards than what
may be given with ,
and ;
.
![]() |
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:
g(A, B) when (A > 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 andalso
is a short-curcuit version of
and
. The keyword orelse
is a short-curcuit version
of or
. They can be used in boolean expressions (not
guards) instead of and
and or
.
Example 1:
case A >= -1.0 andalso math:sqrt(A+1) > B of
This will work even if A
is less than -1.0
, since
in that case, the second term (after andalso
) is never
evaluated. (Of course, the same effects could have been done
using guards. In guards, evaluation is always short-circuited,
since guard tests are known to be free of side-effects.)
Example 2:
OnlyOne = is_atom(L) orelse (is_list(L) andalso length(L) == 1),