aboutsummaryrefslogtreecommitdiffstats
path: root/system/doc/extensions/misc.xml
blob: 576f705278069b9075438ec06563d7cdd771d87c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<?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">&nbsp;</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">&nbsp;</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">- &iquest;</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">&Agrave; - &Ouml;</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">&times;</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">&Oslash; - &THORN;</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">&szlig; - &ouml;</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">&divide;</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">&oslash; - &yuml;</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>