Introduction to Erlang : Basic Types (2/2)

More Data Types

Today we will see some more sophisticated Erlang’s data types; tuple, list, and fun.

Tuple

Tuple is a compound data type; it consists of elements of any data type. A tuple has the form:

{Element1, Element2, ..., ElementN}

where N is called the size the tuple.

1> {1,2}.
{1,2}
2> {true, {value, whatever}}.
{true,{value,whatever}}
3> {1, a, 1.1, {{{{4}}}}}.
{1,a,1.1,{{{{4}}}}}
4> is_tuple({}). % built-in function (BIF)
true
5> element(3, {1,2,3}). % the element at position 3
3
6> size({1,2,3}).% the size of the tuple
3

List

As in all functional programming language, list is one of the most used data type. Again, Erlang borrows the list syntax from Prolog. Because of their importance, we will later have a post (maybe 2) dedicated to lists.

Lists are defined as

[Element1, Element2, ..., ElementN]

where N is called the length of the list.

1> [1,2,3].
[1,2,3]
2> [1,a,{1,2}, 3.14].
[1,a,{1,2},3.14]
3> []. % the empty list
[]
4> is_list([]).
true
5> [1, [2, [3, [a, b, {{{c}}}]]], d].
[1,[2,[3,[a,b,{{{c}}}]]],d]
6> List = [1,2,3], length(List).
3

A list can be “recursively” defined as either the empty list ([]), or a compound structure consisting of the head (the first element of the list) and the tail (the rest of the list), which is a list.
One of the most commonly used operator on lists is the |, which can be used to express the above definition as [Head | Tail]. Therefore:

[] % is a list
[E1 | []] == [E1] % is a list
[E1 | [E2 | []]] == [E1, E2] % is a list
[E1 | [E2 | [...]]] == [E1, E2, ...] % is a list

Notice that the [E1, E2, ..., EN] is a shorthand for [E1 | [E2 | ... | [EN | []]...]].

1> A = [1,2,3].
[1,2,3]
2> [B | C] = [1,2,3]. % pattern matching using |
[1,2,3]
3> B.
1
4> C.
[2,3]
5> [D | E] = [1].
[1]
6> D. 
1
7> E.
[]
8> [F | G] = []. % doesn't pattern match with the empty list
** exception error: no match of right hand side value []
9> [] = [].
[]
10> [] == [].
true

Operations

1> A = [1,2,3].
[1,2,3]
2> [newhead | A].
[newhead,1,2,3]
3> [n1, n2 | A].     
[n1,n2,1,2,3]
4> B = [3,4,5].
[3,4,5]
5> A ++ B. % list concat
[1,2,3,3,4,5]
6> B ++ A.
[3,4,5,1,2,3]
7> A -- B. % list difference
[1,2]
8> [1,1,2,2,3,3] -- [1,2,2,3].
[1,3]

As I wrote before, we will see much more staff about lists in later posts.

Fun(ction)

Erlang supports higher-order functions. In a nutshell, this means that functions can be assigned to variables, be passed as arguments to a function call, and be the return value of a function call (Read more). This is supported by the fun type in Erlang, which is a functional object.

1> Same = fun(X) -> X end.
#Fun<erl_eval .6.13229925>
2> Same(abc).
abc
3> Double = fun(X) -> 2*X end.
#Fun</erl_eval><erl_eval .6.13229925>
4> Double(17).
34
5> Double(17.7).
35.4
6> Apply = fun(X, Fun) -> Fun(X) end.
#Fun</erl_eval><erl_eval .12.113037538>
7> Apply(33, Double).
66
8> Inc = fun(X) -> X+1 end, GiveInc = fun() -> Inc end.
#Fun</erl_eval><erl_eval .20.67289768>
9> GiveInc().
#Fun</erl_eval><erl_eval .6.13229925>
10> (GiveInc())(3).
4
</erl_eval>

Don’t worry about the syntax of a function since we will see how to declare (anonymous) functions soon.

Other types

There are some other types, such as binary, reference, Pid, etc. I will explain them when needed.

Next

In the next post, we will see what is a module and how to create and compile one.

Series NavigationIntroduction to Erlang : Basic Types (1/2)Introduction to Erlang : Modules & Compilation

Leave a Reply

*

Posts’ Calendar
March 2011
M T W T F S S
 123456
78910111213
14151617181920
21222324252627
28293031