[Erlang Systems]

6 Miscellaneous

In this chapter, a number of miscellaneous features of Erlang are described.

6.1 Token Syntax

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
Character classes

6.2 String concatenation

Two adjacent string literals are concatenated into one. This is done already at compile-time, and doesn't incur any run-time overhead. Example:

        "string" "42"

is equivalent to

        "string42"

This feature is convenient in at least two situations:

6.3 The ++ list concatenation operator

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.

6.4 The -- list subtraction operator

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.

6.5 Bitwise operator bnot

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.

returns -8.

6.6 Logical operators

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.

6.7 Match operator = in patterns

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.

6.8 Literal string prefix in patterns

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]) -> ...

6.9 Disjunctions in guards

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.


Copyright © 1991-1999 Ericsson Utvecklings AB