1
<?xml version="1.0" encoding="latin1" ?>
2
<!DOCTYPE chapter SYSTEM "chapter.dtd">
7
<year>2003</year><year>2009</year>
8
<holder>Ericsson AB. All Rights Reserved.</holder>
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/.
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
24
<title>Data Types</title>
29
<file>data_types.xml</file>
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
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>
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>
50
Integer with the base <em><c>base</c></em>, which must be an
51
integer in the range 2..36. <br></br>
53
In Erlang 5.2/OTP R9B and earlier versions, the allowed range
62
3> <input>$\n.</input>
64
4> <input>2#101.</input>
66
5> <input>16#1f.</input>
68
6> <input>2.3.</input>
70
7> <input>2.3e3.</input>
72
8> <input>2.3e-3.</input>
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>
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>
99
1> <input><<10,20>>.</input>
101
2> <input><<"ABC">>.</input>
103
1> <input><<1:1,0:1>>.</input>
105
<p>More examples can be found in Programming Examples.</p>
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>
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>
121
1> <input>Fun1 = fun (X) -> X+1 end.</input>
122
#Fun<erl_eval.6.39074546>
123
2> <input>Fun1(2).</input>
125
<p>Read more about funs in <seealso marker="expressions#funs">Fun Expressions</seealso>. More examples can be found in Programming
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>
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>
143
1> <input>spawn(m, f, []).</input>
145
<p>The BIF <c>self()</c> returns the pid of the calling process.
154
io:format("I am ~p~n", [self()]),
158
1> <input>P = spawn(m, loop, []).</input>
160
2> <input>P ! who_are_you.</input>
163
<p>Read more about processes in
164
<seealso marker="processes">Processes</seealso>.</p>
169
<p>Compound data type with a fixed number of terms:</p>
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>
178
1> <input>P = {adam,24,{july,29}}.</input>
180
2> <input>element(1,P).</input>
182
3> <input>element(3,P).</input>
184
4> <input>P2 = setelement(2,P,25).</input>
186
5> <input>tuple_size(P).</input>
188
6> <input>tuple_size({}).</input>
194
<p>Compound data type with a variable number of terms.</p>
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>
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>
217
1> <input>L1 = [a,2,{c,4}].</input>
219
2> <input>[H|T] = L1.</input>
225
5> <input>L2 = [d|T].</input>
227
6> <input>length(L1).</input>
229
7> <input>length([]).</input>
231
<p>A collection of list processing functions can be found in
232
the STDLIB module <c>lists</c>.</p>
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.
246
<p>is equivalent to</p>
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>
265
-record(person, {name, age}).
268
#person{name=Name, age=Age}.
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>
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>
283
1> <input>2 =< 3</input>.
285
2> <input>true or false</input>.
290
<title>Escape Sequences</title>
291
<p>Within strings and quoted atoms, the following escape sequences
295
<cell align="left" valign="middle"><em>Sequence</em></cell>
296
<cell align="left" valign="middle"><em>Description</em></cell>
299
<cell align="left" valign="middle">\b</cell>
300
<cell align="left" valign="middle">backspace</cell>
303
<cell align="left" valign="middle">\d</cell>
304
<cell align="left" valign="middle">delete</cell>
307
<cell align="left" valign="middle">\e</cell>
308
<cell align="left" valign="middle">escape</cell>
311
<cell align="left" valign="middle">\f</cell>
312
<cell align="left" valign="middle">form feed</cell>
315
<cell align="left" valign="middle">\n</cell>
316
<cell align="left" valign="middle">newline</cell>
319
<cell align="left" valign="middle">\r</cell>
320
<cell align="left" valign="middle">carriage return</cell>
323
<cell align="left" valign="middle">\s</cell>
324
<cell align="left" valign="middle">space</cell>
327
<cell align="left" valign="middle">\t</cell>
328
<cell align="left" valign="middle">tab</cell>
331
<cell align="left" valign="middle">\v</cell>
332
<cell align="left" valign="middle">vertical tab</cell>
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>
339
<cell align="left" valign="middle">\xXY</cell>
340
<cell align="left" valign="middle">character with hexadecimal representation XY</cell>
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>
347
<cell align="left" valign="middle">\^a...\^z <br></br>
349
<cell align="left" valign="middle">control A to control Z</cell>
352
<cell align="left" valign="middle">\'</cell>
353
<cell align="left" valign="middle">single quote</cell>
356
<cell align="left" valign="middle">\"</cell>
357
<cell align="left" valign="middle">double quote</cell>
360
<cell align="left" valign="middle">\\</cell>
361
<cell align="left" valign="middle">backslash</cell>
363
<tcaption>Recognized Escape Sequences.</tcaption>
368
<title>Type Conversions</title>
369
<p>There are a number of BIFs for type conversions. Examples:</p>
371
1> <input>atom_to_list(hello).</input>
373
2> <input>list_to_atom("hello").</input>
375
3> <input>binary_to_list(<<"hello">>).</input>
377
4> <input>binary_to_list(<<104,101,108,108,111>>).</input>
379
5> <input>list_to_binary("hello").</input>
380
<<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>
385
8> <input>integer_to_list(77).</input>
387
9> <input>list_to_integer("77").</input>
389
10> <input>tuple_to_list({a,b,c}).</input>
391
11> <input>list_to_tuple([a,b,c]).</input>
393
12> <input>term_to_binary({a,b,c}).</input>
394
<<131,104,3,100,0,1,97,100,0,1,98,100,0,1,99>>
395
13> <input>binary_to_term(<<131,104,3,100,0,1,97,100,0,1,98,100,0,1,99>>).</input>