~ubuntu-branches/debian/squeeze/erlang/squeeze

« back to all changes in this revision

Viewing changes to system/doc/reference_manual/data_types.xml

  • Committer: Bazaar Package Importer
  • Author(s): Sergei Golovan
  • Date: 2010-03-09 17:34:57 UTC
  • mfrom: (10.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20100309173457-4yd6hlcb2osfhx31
Tags: 1:13.b.4-dfsg-3
Manpages in section 1 are needed even if only arch-dependent packages are
built. So, re-enabled them.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="latin1" ?>
 
2
<!DOCTYPE chapter SYSTEM "chapter.dtd">
 
3
 
 
4
<chapter>
 
5
  <header>
 
6
    <copyright>
 
7
      <year>2003</year><year>2009</year>
 
8
      <holder>Ericsson AB. All Rights Reserved.</holder>
 
9
    </copyright>
 
10
    <legalnotice>
 
11
      The contents of this file are subject to the Erlang Public License,
 
12
      Version 1.1, (the "License"); you may not use this file except in
 
13
      compliance with the License. You should have received a copy of the
 
14
      Erlang Public License along with this software. If not, it can be
 
15
      retrieved online at http://www.erlang.org/.
 
16
    
 
17
      Software distributed under the License is distributed on an "AS IS"
 
18
      basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 
19
      the License for the specific language governing rights and limitations
 
20
      under the License.
 
21
    
 
22
    </legalnotice>
 
23
 
 
24
    <title>Data Types</title>
 
25
    <prepared></prepared>
 
26
    <docno></docno>
 
27
    <date></date>
 
28
    <rev></rev>
 
29
    <file>data_types.xml</file>
 
30
  </header>
 
31
 
 
32
  <section>
 
33
    <title>Terms</title>
 
34
    <p>Erlang provides a number of data types which are listed in this
 
35
      chapter. A piece of data of any data type is called a
 
36
      <em>term</em>.</p>
 
37
  </section>
 
38
 
 
39
  <section>
 
40
    <title>Number</title>
 
41
    <p>There are two types of numeric literals, <em>integers</em> and
 
42
      <em>floats</em>. Besides the conventional notation, there are two
 
43
      Erlang-specific notations:</p>
 
44
    <list type="bulleted">
 
45
      <item><c>$</c><em><c>char</c></em>      <br></br>
 
46
 
 
47
       ASCII value of the character <em><c>char</c></em>.</item>
 
48
      <item><em><c>base</c></em><c>#</c><em><c>value</c></em>      <br></br>
 
49
 
 
50
       Integer with the base <em><c>base</c></em>, which must be an
 
51
       integer in the range 2..36.      <br></br>
 
52
 
 
53
       In Erlang 5.2/OTP R9B and earlier versions, the allowed range
 
54
       is 2..16.</item>
 
55
    </list>
 
56
    <p>Examples:</p>
 
57
    <pre>
 
58
1> <input>42.</input>
 
59
42
 
60
2> <input>$A.</input>
 
61
65
 
62
3> <input>$\n.</input>
 
63
10
 
64
4> <input>2#101.</input>
 
65
5
 
66
5> <input>16#1f.</input>
 
67
31
 
68
6> <input>2.3.</input>
 
69
2.3
 
70
7> <input>2.3e3.</input>
 
71
2.3e3
 
72
8> <input>2.3e-3.</input>
 
73
0.0023</pre>
 
74
  </section>
 
75
 
 
76
  <section>
 
77
    <title>Atom</title>
 
78
    <p>An atom is a literal, a constant with name. An atom should be
 
79
      enclosed in single quotes (') if it does not begin with a
 
80
      lower-case letter or if it contains other characters than
 
81
      alphanumeric characters, underscore (_), or @.</p>
 
82
    <p>Examples:</p>
 
83
    <pre>
 
84
hello
 
85
phone_number
 
86
'Monday'
 
87
'phone number'</pre>
 
88
  </section>
 
89
 
 
90
  <section>
 
91
    <title>Bit Strings and Binaries</title>
 
92
    <p>A bit string is used to store an area of untyped memory.</p>
 
93
    <p>Bit Strings are expressed using the
 
94
      <seealso marker="expressions#bit_syntax">bit syntax</seealso>.</p>
 
95
    <p>Bit Strings which consists of a number of bits which is evenly 
 
96
      divisible by eight are called Binaries</p>
 
97
    <p>Examples:</p>
 
98
    <pre>
 
99
1> <input>&lt;&lt;10,20&gt;&gt;.</input>
 
100
&lt;&lt;10,20>>
 
101
2> <input>&lt;&lt;"ABC"&gt;&gt;.</input>
 
102
&lt;&lt;"ABC">>
 
103
1> <input>&lt;&lt;1:1,0:1&gt;&gt;.</input>
 
104
&lt;&lt;2:2>></pre>
 
105
    <p>More examples can be found in Programming Examples.</p>
 
106
  </section>
 
107
 
 
108
  <section>
 
109
    <title>Reference</title>
 
110
    <p>A reference is a term which is unique in an Erlang runtime
 
111
      system, created by calling <c>make_ref/0</c>.</p>
 
112
  </section>
 
113
 
 
114
  <section>
 
115
    <title>Fun</title>
 
116
    <p>A fun is a functional object. Funs make it possible to create
 
117
      an anonymous function and pass the function itself -- not its
 
118
      name -- as argument to other functions.</p>
 
119
    <p>Example:</p>
 
120
    <pre>
 
121
1> <input>Fun1 = fun (X) -> X+1 end.</input>
 
122
#Fun&lt;erl_eval.6.39074546&gt;
 
123
2> <input>Fun1(2).</input>
 
124
3</pre>
 
125
    <p>Read more about funs in <seealso marker="expressions#funs">Fun Expressions</seealso>. More examples can be found in Programming
 
126
      Examples.</p>
 
127
  </section>
 
128
 
 
129
  <section>
 
130
    <title>Port Identifier</title>
 
131
    <p>A port identifier identifies an Erlang port. <c>open_port/2</c>,
 
132
      which is used to create ports, will return a value of this type.</p>
 
133
    <p>Read more about ports in <seealso marker="ports">Ports and Port Drivers</seealso>.</p>
 
134
  </section>
 
135
 
 
136
  <section>
 
137
    <title>Pid</title>
 
138
    <p>A process identifier, pid, identifies a process.
 
139
      <c>spawn/1,2,3,4</c>, <c>spawn_link/1,2,3,4</c> and
 
140
      <c>spawn_opt/4</c>, which are used to create processes, return
 
141
      values of this type. Example:</p>
 
142
    <pre>
 
143
1> <input>spawn(m, f, []).</input>
 
144
&lt;0.51.0></pre>
 
145
    <p>The BIF <c>self()</c> returns the pid of the calling process.
 
146
      Example:</p>
 
147
    <pre>
 
148
-module(m).
 
149
-export([loop/0]).
 
150
 
 
151
loop() ->
 
152
    receive
 
153
        who_are_you ->
 
154
            io:format("I am ~p~n", [self()]),
 
155
            loop()
 
156
    end.
 
157
 
 
158
1> <input>P = spawn(m, loop, []).</input>
 
159
&lt;0.58.0>
 
160
2> <input>P ! who_are_you.</input>
 
161
I am &lt;0.58.0>
 
162
who_are_you</pre>
 
163
    <p>Read more about processes in
 
164
      <seealso marker="processes">Processes</seealso>.</p>
 
165
  </section>
 
166
 
 
167
  <section>
 
168
    <title>Tuple</title>
 
169
    <p>Compound data type with a fixed number of terms:</p>
 
170
    <pre>
 
171
{Term1,...,TermN}</pre>
 
172
    <p>Each term <c>Term</c> in the tuple is called an
 
173
      <em>element</em>. The number of elements is said to be
 
174
      the <em>size</em> of the tuple.</p>
 
175
    <p>There exists a number of BIFs to manipulate tuples.</p>
 
176
    <p>Examples:</p>
 
177
    <pre>
 
178
1> <input>P = {adam,24,{july,29}}.</input>
 
179
{adam,24,{july,29}}
 
180
2> <input>element(1,P).</input>
 
181
adam
 
182
3> <input>element(3,P).</input>
 
183
{july,29}
 
184
4> <input>P2 = setelement(2,P,25).</input>
 
185
{adam,25,{july,29}}
 
186
5> <input>tuple_size(P).</input>
 
187
3
 
188
6> <input>tuple_size({}).</input>
 
189
0</pre>
 
190
  </section>
 
191
 
 
192
  <section>
 
193
    <title>List</title>
 
194
    <p>Compound data type with a variable number of terms.</p>
 
195
    <pre>
 
196
[Term1,...,TermN]</pre>
 
197
    <p>Each term <c>Term</c> in the list is called an
 
198
      <em>element</em>. The number of elements is said to be
 
199
      the <em>length</em> of the list.</p>
 
200
    <p>Formally, a list is either the empty list <c>[]</c> or
 
201
      consists of a <em>head</em> (first element) and a <em>tail</em>
 
202
      (remainder of the list) which is also a list. The latter can
 
203
      be expressed as <c>[H|T]</c>. The notation
 
204
      <c>[Term1,...,TermN]</c> above is actually shorthand for
 
205
      the list <c>[Term1|[...|[TermN|[]]]]</c>.</p>
 
206
    <p>Example:      <br></br>
 
207
<c>[]</c> is a list, thus      <br></br>
 
208
<c>[c|[]]</c> is a list, thus      <br></br>
 
209
<c>[b|[c|[]]]</c> is a list, thus      <br></br>
 
210
<c>[a|[b|[c|[]]]]</c> is a list, or in short <c>[a,b,c]</c>.</p>
 
211
    <p></p>
 
212
    <p>A list where the tail is a list is sometimes called a <em>proper list</em>. It is allowed to have a list where the tail is not a
 
213
      list, for example <c>[a|b]</c>. However, this type of list is of
 
214
      little practical use.</p>
 
215
    <p>Examples:</p>
 
216
    <pre>
 
217
1> <input>L1 = [a,2,{c,4}].</input>
 
218
[a,2,{c,4}]
 
219
2> <input>[H|T] = L1.</input>
 
220
[a,2,{c,4}]
 
221
3> <input>H.</input>
 
222
a
 
223
4> <input>T.</input>
 
224
[2,{c,4}]
 
225
5> <input>L2 = [d|T].</input>
 
226
[d,2,{c,4}]
 
227
6> <input>length(L1).</input>
 
228
3
 
229
7> <input>length([]).</input>
 
230
0</pre>
 
231
    <p>A collection of list processing functions can be found in
 
232
      the STDLIB module <c>lists</c>.</p>
 
233
  </section>
 
234
 
 
235
  <section>
 
236
    <title>String</title>
 
237
    <p>Strings are enclosed in double quotes ("), but is not a
 
238
      data type in Erlang. Instead a string <c>"hello"</c> is
 
239
      shorthand for the list <c>[$h,$e,$l,$l,$o]</c>, that is
 
240
      <c>[104,101,108,108,111]</c>.</p>
 
241
    <p>Two adjacent string literals are concatenated into one. This is
 
242
      done at compile-time and does not incur any runtime overhead.
 
243
      Example:</p>
 
244
    <pre>
 
245
"string" "42"</pre>
 
246
    <p>is equivalent to</p>
 
247
    <pre>
 
248
"string42"</pre>
 
249
  </section>
 
250
 
 
251
  <section>
 
252
    <title>Record</title>
 
253
    <p>A record is a data structure for storing a fixed number of
 
254
      elements. It has named fields and is similar to a struct in C.
 
255
      However, record is not a true data type. Instead record
 
256
      expressions are translated to tuple expressions during
 
257
      compilation. Therefore, record expressions are not understood by
 
258
      the shell unless special actions are taken. See <c>shell(3)</c>
 
259
      for details.</p>
 
260
    <p>Examples:</p>
 
261
    <pre>
 
262
-module(person).
 
263
-export([new/2]).
 
264
 
 
265
-record(person, {name, age}).
 
266
 
 
267
new(Name, Age) ->
 
268
    #person{name=Name, age=Age}.
 
269
 
 
270
1> <input>person:new(ernie, 44).</input>
 
271
{person,ernie,44}</pre>
 
272
    <p>Read more about records in
 
273
      <seealso marker="records">Records</seealso>. More examples can be
 
274
      found in Programming Examples.</p>
 
275
  </section>
 
276
 
 
277
  <section>
 
278
    <title>Boolean</title>
 
279
    <p>There is no Boolean data type in Erlang. Instead the atoms
 
280
      <c>true</c> and <c>false</c> are used to denote Boolean values.</p>
 
281
    <p>Examples:</p>
 
282
    <pre>
 
283
1> <input>2 =&lt; 3</input>.
 
284
true
 
285
2> <input>true or false</input>.
 
286
true</pre>
 
287
  </section>
 
288
 
 
289
  <section>
 
290
    <title>Escape Sequences</title>
 
291
    <p>Within strings and quoted atoms, the following escape sequences
 
292
      are recognized:</p>
 
293
    <table>
 
294
      <row>
 
295
        <cell align="left" valign="middle"><em>Sequence</em></cell>
 
296
        <cell align="left" valign="middle"><em>Description</em></cell>
 
297
      </row>
 
298
      <row>
 
299
        <cell align="left" valign="middle">\b</cell>
 
300
        <cell align="left" valign="middle">backspace</cell>
 
301
      </row>
 
302
      <row>
 
303
        <cell align="left" valign="middle">\d</cell>
 
304
        <cell align="left" valign="middle">delete</cell>
 
305
      </row>
 
306
      <row>
 
307
        <cell align="left" valign="middle">\e</cell>
 
308
        <cell align="left" valign="middle">escape</cell>
 
309
      </row>
 
310
      <row>
 
311
        <cell align="left" valign="middle">\f</cell>
 
312
        <cell align="left" valign="middle">form feed</cell>
 
313
      </row>
 
314
      <row>
 
315
        <cell align="left" valign="middle">\n</cell>
 
316
        <cell align="left" valign="middle">newline</cell>
 
317
      </row>
 
318
      <row>
 
319
        <cell align="left" valign="middle">\r</cell>
 
320
        <cell align="left" valign="middle">carriage return</cell>
 
321
      </row>
 
322
      <row>
 
323
        <cell align="left" valign="middle">\s</cell>
 
324
        <cell align="left" valign="middle">space</cell>
 
325
      </row>
 
326
      <row>
 
327
        <cell align="left" valign="middle">\t</cell>
 
328
        <cell align="left" valign="middle">tab</cell>
 
329
      </row>
 
330
      <row>
 
331
        <cell align="left" valign="middle">\v</cell>
 
332
        <cell align="left" valign="middle">vertical tab</cell>
 
333
      </row>
 
334
      <row>
 
335
        <cell align="left" valign="middle">\XYZ, \YZ, \Z</cell>
 
336
        <cell align="left" valign="middle">character with octal representation XYZ, YZ or Z</cell>
 
337
      </row>
 
338
      <row>
 
339
        <cell align="left" valign="middle">\xXY</cell>
 
340
        <cell align="left" valign="middle">character with hexadecimal representation XY</cell>
 
341
      </row>
 
342
      <row>
 
343
        <cell align="left" valign="middle">\x{X...}</cell>
 
344
        <cell align="left" valign="middle">character with hexadecimal representation; X... is one or more hexadecimal characters</cell>
 
345
      </row>
 
346
      <row>
 
347
        <cell align="left" valign="middle">\^a...\^z        <br></br>
 
348
\^A...\^Z</cell>
 
349
        <cell align="left" valign="middle">control A to control Z</cell>
 
350
      </row>
 
351
      <row>
 
352
        <cell align="left" valign="middle">\'</cell>
 
353
        <cell align="left" valign="middle">single quote</cell>
 
354
      </row>
 
355
      <row>
 
356
        <cell align="left" valign="middle">\"</cell>
 
357
        <cell align="left" valign="middle">double quote</cell>
 
358
      </row>
 
359
      <row>
 
360
        <cell align="left" valign="middle">\\</cell>
 
361
        <cell align="left" valign="middle">backslash</cell>
 
362
      </row>
 
363
      <tcaption>Recognized Escape Sequences.</tcaption>
 
364
    </table>
 
365
  </section>
 
366
 
 
367
  <section>
 
368
    <title>Type Conversions</title>
 
369
    <p>There are a number of BIFs for type conversions. Examples:</p>
 
370
    <pre>
 
371
1> <input>atom_to_list(hello).</input>
 
372
"hello"
 
373
2> <input>list_to_atom("hello").</input>
 
374
hello
 
375
3> <input>binary_to_list(&lt;&lt;"hello">>).</input>
 
376
"hello"
 
377
4> <input>binary_to_list(&lt;&lt;104,101,108,108,111>>).</input>
 
378
"hello"
 
379
5> <input>list_to_binary("hello").</input>
 
380
&lt;&lt;104,101,108,108,111>>
 
381
6> <input>float_to_list(7.0).</input>
 
382
"7.00000000000000000000e+00"
 
383
7> <input>list_to_float("7.000e+00").</input>
 
384
7.0
 
385
8> <input>integer_to_list(77).</input>
 
386
"77"
 
387
9> <input>list_to_integer("77").</input>
 
388
77
 
389
10> <input>tuple_to_list({a,b,c}).</input>
 
390
[a,b,c]
 
391
11> <input>list_to_tuple([a,b,c]).</input>
 
392
{a,b,c}
 
393
12> <input>term_to_binary({a,b,c}).</input>
 
394
&lt;&lt;131,104,3,100,0,1,97,100,0,1,98,100,0,1,99>>
 
395
13> <input>binary_to_term(&lt;&lt;131,104,3,100,0,1,97,100,0,1,98,100,0,1,99>>).</input>
 
396
{a,b,c}</pre>
 
397
  </section>
 
398
</chapter>
 
399