Erlang provides the following data types:
- Constant data types – these are data types which cannot be split into more primitive subtypes:
- Numbers – for example:
123
,-789
,3.14159
,7.8e12
,-1.2e-45
. Numbers are further subdivided into integers and floats. - Atoms – for example:
abc
,'An atom with spaces'
,monday
,green
,hello_world
. These are simply constants with names.
- Numbers – for example:
- Compound data types – these are used to group together other data types. There are two compound data types:
- Tuples – for example:
{a, 12, b}
,{}
,{1, 2, 3}
,{a, b, c, d, e}
. Tuples are used for storing a fixed number of items and are written as sequences of items enclosed in curly brackets. Tuples are similar to records or structures in conventional programming languages. - Lists – for example:
[]
,[a, b, 12]
,[22]
,[a, 'hello friend']
. Lists are used for storing a variable number of items and are written as sequences of items enclosed in square brackets.
- Tuples – for example:
Components of tuples and lists can themselves be any Erlang data item – this allows us to create arbitrary complex structures.
The values of Erlang data types can be stored in variables. Variables always start with an upper-case letter so, for example, the code fragment:
X = {book, preface, acknowledgments, contents,
{chapters, [
{chapter, 1, 'An Erlang Tutorial'},
{chapter, 2, ...}
]
}},
creates a complex data structure and stores it in the variable X
.