The data types seem strange to a "C" programmer. Let us attack one by one
1. Terms
A piece of data of any data type is called a term.
2. Number
There are two types of numeric literals,
- integers
- floats.
Besides the conventional notation, there are two Erlang-specific notations:
- $char - ASCII value of the character char.
- base#value - Integer with the base base, which must be an integer in the range 2..36.
42
$A (evaluates to 65)
$\n (10)
2#101 (5)
16#1f (31)
2.3
2.3e3 (2300.00)
2.3e-3 (2.30000e-3)
3 Atom
An atom is a literal, a constant with name. An atom should be enclosed in single quotes (') if it does not begin with a lower-case letter or if it contains other characters than alphanumeric characters, underscore (_), or @.
Examples:
hello
phone_number
'Monday'
'phone number'
4 Binary
A binary is used to store an area of untyped memory.
Binaries are expressed using the bit syntax.
<<10,20>>
<<"ABC">>
5 Reference
A reference is a term which is unique in an Erlang runtime system, created by calling make_ref/0.
6 Fun
A fun is a functional object. Funs make it possible to create an anonymous function and pass the function itself -- not its name -- as argument to other functions.
Example:
Fun1 = fun (X) -> X+1 end.
7 Port Identifier
A port identifier identifies an Erlang port. open_port/2, which is used to create ports, will return a value of this type.
8 Pid
A process identifier, pid, identifies a process. spawn/1,2,3,4, spawn_link/1,2,3,4 and spawn_opt/4, which are used to create processes, return values of this type.
Example:
spawn(m, f, []) returns <0.51.0>
9 Tuple
Compound data type with a fixed number of terms: {Term1,...,TermN}
Each term Term in the tuple is called an element. The number of elements is said to be the size of the tuple.
Examples:
P = {adam,24,{july,29}}
10 List
Compound data type with a variable number of terms. [Term1,...,TermN]
Each term Term in the list is called an element. The number of elements is said to be the length of the list.
Formally, a list is either the empty list [] or consists of a head (first element) and a tail (remainder of the list) which is also a list.
The latter can be expressed as [HT].
The notation [Term1,...,TermN] above is actually shorthand for the list [Term1[...[TermN[]]]].
Example:
[] is a list,
thus[c[]] is a list,
thus[b[c[]]] is a list,
thus[a[b[c[]]]] is a list, or
in short [a,b,c].
11 String
Strings are enclosed in double quotes ("), but is not a data type in Erlang.
Instead a string "hello" is shorthand for the list [$h,$e,$l,$l,$o], that is [104,101,108,108,111].
Two adjacent string literals are concatenated into one. This is done at compile-time and does not incur any runtime overhead.
Example:
"string" "42" is equivalent to "string42"
12 Record
A record is a data structure for storing a fixed number of elements. It has named fields and is similar to a struct in C. However, record is not a true data type. Instead record expressions are translated to tuple expressions during compilation. Therefore, record expressions are not understood by the shell unless special actions are taken.
Examples:
-module(person).
-export([new/2]).
-record(person, {name, age}).
new(Name, Age) ->
#person{name=Name, age=Age}.
13 Boolean
There is no Boolean data type in Erlang. Instead the atoms true and false are used to denote Boolean values.
Examples:
2=<3 evaluates to true
true or false evaluates to true
14 Escape Sequences
Within strings and quoted atoms, the following escape sequences are recognized:
\b - backspace
\d - delete
\e - escape
\f - form feed
\n - newline
\r - carriage return
\s - space
\t - tab
\v - vertical tab
\XYZ, \YZ, \Z - character with octal representation XYZ, YZ or Z
\^a...\^z\^A...\^Z - control A to control Z
\' - single quote
\" - double quote
\\ - backslash
There are a number of build-in-functions (BIFs) for type conversions.
Examples:
atom_to_list(hello) evaluates to "hello"
list_to_atom("hello") evaluates to hello
binary_to_list(<<"hello">>) evaluates to "hello"
binary_to_list(<<104,101,108,108,111>>) evaluates to "hello"
list_to_binary("hello") evaluates to <<104,101,108,108,111>>
float_to_list(7.0) evaluates to "7.00000000000000000000e+00"
list_to_float("7.000e+00") evaluates to 7.00000
integer_to_list(77) evaluates to "77"
list_to_integer("77") evaluates to 77
tuple_to_list({a,b,c}) evaluates to [a,b,c]
list_to_tuple([a,b,c]) evaluates to {a,b,c}
term_to_binary({a,b,c}) evaluates to <<131,104,3,100,0,1,97,100,0,1,98,100,0,1,99>>
binary_to_term(<<131,104,3,100,0,1,97,100,0,1,98,100,0,1,99>>) evaluates to {a,b,c}
No comments:
Post a Comment