<?xml version="1.0" encoding="latin1" ?>
<!DOCTYPE chapter SYSTEM "chapter.dtd">
<chapter>
<header>
<copyright>
<year>1999</year><year>2009</year>
<holder>Ericsson AB. All Rights Reserved.</holder>
</copyright>
<legalnotice>
The contents of this file are subject to the Erlang Public License,
Version 1.1, (the "License"); you may not use this file except in
compliance with the License. You should have received a copy of the
Erlang Public License along with this software. If not, it can be
retrieved online at http://www.erlang.org/.
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
the License for the specific language governing rights and limitations
under the License.
</legalnotice>
<title>Miscellaneous</title>
<prepared>Arndt Jonasson</prepared>
<docno>1</docno>
<date>99-01-25</date>
<rev>PA1</rev>
<file>misc.sgml</file>
</header>
<p>In this chapter, a number of miscellaneous features of Erlang
are described.</p>
<section>
<title>Token Syntax</title>
<p>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:</p>
<list type="bulleted">
<item>All the Latin-1 printable characters can be used and are shown without
the escape backslash convention.</item>
<item>Atoms and variables can use all Latin-1 letters.</item>
</list>
<p>The new characters from Latin-1 have the following
classifications in Erlang:</p>
<table>
<row>
<cell align="left" valign="middle"><em>Octal</em></cell>
<cell align="left" valign="middle"><em>Decimal</em></cell>
<cell align="left" valign="middle"> </cell>
<cell align="left" valign="middle"><em>Class</em></cell>
</row>
<row>
<cell align="left" valign="middle">200 - 237</cell>
<cell align="left" valign="middle">128 - 159</cell>
<cell align="left" valign="middle"> </cell>
<cell align="left" valign="middle">Control characters</cell>
</row>
<row>
<cell align="left" valign="middle">240 - 277</cell>
<cell align="left" valign="middle">160 - 191</cell>
<cell align="right" valign="middle">- ¿</cell>
<cell align="left" valign="middle">Punctuation characters</cell>
</row>
<row>
<cell align="left" valign="middle">300 - 326</cell>
<cell align="left" valign="middle">192 - 214</cell>
<cell align="center" valign="middle">À - Ö</cell>
<cell align="left" valign="middle">Uppercase letters</cell>
</row>
<row>
<cell align="center" valign="middle">327</cell>
<cell align="center" valign="middle">215</cell>
<cell align="center" valign="middle">×</cell>
<cell align="left" valign="middle">Punctuation character</cell>
</row>
<row>
<cell align="left" valign="middle">330 - 336</cell>
<cell align="left" valign="middle">216 - 222</cell>
<cell align="center" valign="middle">Ø - Þ</cell>
<cell align="left" valign="middle">Uppercase letters</cell>
</row>
<row>
<cell align="left" valign="middle">337 - 366</cell>
<cell align="left" valign="middle">223 - 246</cell>
<cell align="center" valign="middle">ß - ö</cell>
<cell align="left" valign="middle">Lowercase letters</cell>
</row>
<row>
<cell align="center" valign="middle">367</cell>
<cell align="center" valign="middle">247</cell>
<cell align="center" valign="middle">÷</cell>
<cell align="left" valign="middle">Punctuation character</cell>
</row>
<row>
<cell align="left" valign="middle">370 - 377</cell>
<cell align="left" valign="middle">248 - 255</cell>
<cell align="center" valign="middle">ø - ÿ</cell>
<cell align="left" valign="middle">Lowercase letters</cell>
</row>
<tcaption>Character classes</tcaption>
</table>
</section>
<section>
<title>String Concatenation</title>
<p>Two adjacent string literals are concatenated into one. This is done already
at compile-time, and doesn't incur any runtime overhead. Example:</p>
<code type="none">
"string" "42" </code>
<p>is equivalent to</p>
<code type="none">
"string42" </code>
<p>This feature is convenient in at least two situations:</p>
<list type="bulleted">
<item>when one of the
strings is the result of a macro expansion;</item>
<item>when a string is very
long, and would otherwise either have to wrap, making the source code
harder to read, or force the use of some runtime append operation.</item>
</list>
</section>
<section>
<title>The ++ list Concatenation Operator</title>
<p>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:
</p>
<code type="none">
X = [1,2,3],
Y = [4,5],
X ++ Y. </code>
<p>results in <c>[1,2,3,4,5]</c>.</p>
<p>The ++ operator has precedence between the binary '+' operator and
the comparison operators.
</p>
</section>
<section>
<title>The - - list Subtraction Operator</title>
<p>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.</p>
<code type="none">
X = [1,2,3,2,1,2],
Y = [2,1,2],
X -- Y. </code>
<p>results in <c>[3,1,2]</c>.</p>
<p>The - - operator has precedence between the binary '+' operator and
the comparison operators.
</p>
</section>
<section>
<title>Bitwise Operator bnot</title>
<p>Apart from the binary bitwise operators <c>band</c>, <c>bor</c>
and <c>bxor</c>, there is a unary operator <c>bnot</c> with the same
precedence as the other unary operators + and -, i.e., higher than
the binary operators. Example:</p>
<code type="none">
bnot 7. </code>
<p>returns -8.
</p>
</section>
<section>
<title>Logical Operators</title>
<p>The atoms <c>true</c> and <c>false</c> are usually used for representing
Boolean values. With the binary operators <c>and</c>, <c>or</c> and
<c>xor</c>, and the unary operator <c>not</c>, Boolean values can be
combined. Example:</p>
<code type="none">
M1 = lists:member(A, List1),
M2 = lists:member(A, List2),
M1 and M2.</code>
<p>Note that the operators are strict, i.e., they always evaluate both
their arguments.</p>
<p><c>not</c> has the same priority as the other unary operators. The
binary logical operators have precedence between the <c>=</c> operator
and the comparison operators, the <c>and</c> operator having higher
precedence than <c>or</c> and <c>xor</c>.
</p>
</section>
<section>
<title>Match Operator = In Patterns</title>
<p>This extension was added in Erlang 4.8 (OTP R5A).</p>
<p>The = operator is also called the `match' operator. The match operator
can now be used in a pattern, so that <c>P1 = P2</c> is a valid pattern,
where both <c>P1</c> and <c>P2</c> are patterns. This compound pattern
when matched against a term causes the term to be matched against both
<c>P1</c> and <c>P2</c>.</p>
<p>One use for this construction is to avoid reconstructing a term which
was part of an argument to a function. Example:</p>
<code type="none">
f({'+',X,Y}=T) -> {X+Y,T}.</code>
<p>It also makes it possible to rewrite the construction</p>
<code type="none">
f(X) when X == #rec{x=1, y=a} -> ... </code>
<p>as</p>
<code type="none">
f(#rec{x=1, y=a} = X) -> ... </code>
<p>In the absence of optimization for the former case, the
latter case is more efficient.
</p>
</section>
<section>
<title>Literal String Prefix in Patterns</title>
<p>This extension was added in Erlang 4.8 (OTP R5A).</p>
<p>A new construction is allowed in patterns, namely a literal
string as the first operand of the ++ operator. Example:</p>
<code type="none">
f("prefix" ++ L) -> ... </code>
<p>This is syntactic sugar for the equivalent, but harder to read</p>
<code type="none">
f([$p,$r,$e,$f,$i,$x | L]) -> ... </code>
</section>
<section>
<title>Disjunctions in Guards</title>
<p>This extension was added in Erlang 4.9 (OTP R6A).</p>
<p>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.</p>
<code type="none">
f(X) when xxx ; yyy ; zzz ->
pop(X).</code>
<p>This is syntactic sugar for the equivalent</p>
<code type="none">
f(X) when xxx ->
pop(X);
f(X) when yyy ->
pop(X);
f(X) when zzz ->
pop(X). </code>
<p>The abstract format has been changed accordingly to contain a list of
(conjunctive) guards where there was previously only one guard.
</p>
</section>
<section>
<title>Expressions in Patterns</title>
<p>This extension was added in Erlang 5.0 (OTP R7A).</p>
<p>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.
</p>
<p>Example:</p>
<code type="none">
case X of
{1+2, T} -> T
end.</code>
</section>
<section>
<title>Boolean expresions in guards</title>
<p>This extension was added in Erlang 5.1 (OTP R8).</p>
<p>In guards, the use of <c>and</c>, <c>or</c> and <c>not</c> is
now allowed. Guard expressions can combine these with
parenthesis. This allows for more elaborate guards than what
may be given with <c>,</c> and <c>;</c>.</p>
<note>
<p>The guard expressions written with these operators are boolean
expressions, and the boolean functions <c>is_list</c>,
<c>is_integer</c> etc. should be used, rather than
<c>list</c>, <c>integer</c> etc.</p>
</note>
<p>Example 1:</p>
<code type="none">
f(X) when not (is_tuple(X) or is_list(X)) ->
... </code>
<p>Example 2:</p>
<code type="none"><![CDATA[
g(A, B) when (A > 0) and (B > 0) and not (A*A < B*B) ->
... ]]></code>
</section>
<section>
<title>Short-circuit boolean expressions</title>
<p>This extension was added in Erlang 5.1 (OTP R8).</p>
<p>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.)</p>
<p>The keyword <c>andalso</c> is a short-curcuit version of
<c>and</c>. The keyword <c>orelse</c> is a short-curcuit version
of <c>or</c>. They can be used in boolean expressions (not
guards) instead of <c>and</c> and <c>or</c>.</p>
<p>Example 1:</p>
<code type="none">
case A >= -1.0 andalso math:sqrt(A+1) > B of </code>
<p>This will work even if <c>A</c> is less than <c>-1.0</c>, since
in that case, the second term (after <c>andalso</c>) 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.)</p>
<p>Example 2:</p>
<code type="none">
OnlyOne = is_atom(L) orelse
(is_list(L) andalso length(L) == 1), </code>
</section>
</chapter>