← Getting Started | Pattern Matching →

The basic unit of expressing a value in Erlang is the term. Terms are comprised of one of Erlang's simple or complex types.

Integers edit

Integer constants are written as numbers, optionally prefixed with a base and the octothorpe (#):

 1> 2.
 2
 2> 16#ff.
 255
 3> 2#1011.
 11

Erlang uses arbitrary-precision integers, which support integers with any number of digits.

Erlang also provides another syntactic shortcut: you can write an integer as the dollar sign ($) and a character, and the value is the ASCII value of that character. This is of use when handling strings, as strings are typically represented in Erlang as lists of integers (see Strings, below).

Floats edit

Floats (floating-point numbers) are written as numbers with decimal places, optionally with an exponent separated from the number with the letter e.

 4> 1.2.
 1.20000
 5> 1.0.
 1.00000
 6> 2.0e-4.
 2.00000e-4

Atoms edit

Atoms are named constants. Atoms begin with a lower-case letter and can contain letters, digits and the underscore character (_); or they are quoted with single quotes (').

 1> ok.
 ok
 2> 'OKAY'.
 'OKAY'
 3> this_is_an_atom.
 this_is_an_atom

The special atoms true and false represent boolean values.

Atoms are often used as the keys in key-value pairs, indicators of success and failure (e.g. ok and error) and to identify parts of a complicated structure for Pattern Matching.

Tuples edit

Tuples are terms composed of multiple values, and are of fixed length. Tuples are surrounded by braces ({ and }), and the elements of the tuple (any Erlang term, including tuples) are separated by commas.

 1> {ok, 9}.
 {ok,9}
 2> {true, {127, 0, 0, 1}}.
 {true,{127,0,0,1}}
 3> {box, {width, 10}, {height, 35}}.
 {box, {width, 10}, {height, 35}}

The 3rd example shows something called a tagged tuple. Where possible use tagged tuples as they make it clear what the tuple is supposed to represent. Later on we will look at records which is just a shorthand for tagged tuples.

Lists edit

Lists are terms composed of multiple values, of varying length. Lists are surrounded by brackets ([ and ]) and each member of the list (any Erlang term, including lists) separated with commas.

Lists can also be composed of a head and tail portion, separated by the vertical bar character (|): [Head|Tail]. The Tail can be any valid Erlang term, but is usually a list representing the members of the list after the head.

 1> [one, two, three].
 [one,two,three]
 2> [1, 2|[3, 4, 5]].
 [1,2,3,4,5]
 3> [{key1, value1}, {key2, value2}].
 [{key1,value1},{key2,value2}]

Lists in which the smallest possible tail is the empty list ([]) are known as well-formed lists.

Strings edit

Erlang has no separate string type. Strings are usually represented by lists of integers (and the string module of the standard library manipulates such lists). Each integer represents the ASCII (or other character set encoding) value of the character in the string. For convenience, a string of characters enclosed in double quotes (") is equivalent to a list of the numerical values of those characters.

 1> "one".
 "one"
 2> [$o, $n, $e].
 "one"
 3> $o.
 111
 4> $n.
 110
 5> $e.
 101
 6> [111, 110, 101].
 "one"

The Erlang shell "guesses" whether a given list is a printable string and prints it that way for convenience.

Pids edit

A Pid or process id is a special type in Erlang. To see what one looks like you can get the pid of yourself with self().

1> self().
  <0.29.0>

Refs edit

A ref (or reference) is a term which is unique, even across Erlang nodes. You can create a new ref by calling erlang:make_ref(). A reference is only used as a unique tag or identifier. An Erlang reference should not be confused with a reference in C/C++.

2> erlang:make_ref().
#Ref<0.0.0.41>

Other types edit

Erlang has other types, such as funs (closures), bit-strings, binaries (continuous blocks of arbitrary data) and ports (port identifiers), that will be covered in their appropriate sections.