Erlang Essentials
Vick Preston
Contents
Erlang Shell 1
2.Data Types in Erlang 3
2.2.Binaries and bitstrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.3.Atoms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.4.Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.5.Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.6.Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.7. Pids, ports, and references . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.8.Functions as data: funs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.9. Comparing Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.10 Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Core commands to remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.Modules and Functions 8
4. Variables and Pattern matching 9
6. Cases and Expressions 11
7. Funs 13
8.Exceptions, try, and catch 14
8.1 Throwing (raising) exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
9. Records 15
10. Preprocessing and include files 16
11. Processes 18
12. ETS Tables 19
13. Recursion - Function that calls itself 20
Erlang Shell
The Erlang shell is an interactive REPL(Read, Evaluate, Print, Loop). It enables execution of Erlang
expressions, test code, spawn processes, inspect state, compile modules, debug interactively. starts in :
erl
Erlang/OTP 28 [...]
Eshell V28
1
,1>
Everything in Erlang is an expression. The expressions are evaluated and values are returned.
1> 2 + 3.
5
The period matters. Erlang shell runs in two modes: - Normal mode ; which commands can be edited and
expressions evaluated - Job Control Mode (JCL); Jobs can be started, killed, detached or connected.
Erlang Shell Commands Cheat Sheet
Command Description Example
erl Starts the Erlang shell erl
q(). Quits the shell q().
init:stop(). Gracefully shuts down Erlang VM init:stop().
h(). Displays shell help h().
h(Module). Shows module help h(lists).
h(Module, Function). Shows help for specific function h(lists, map).
f(). Clears all variable bindings f().
f(Var). Clears specific variable binding f(X).
v(N). Retrieves result of shell command v(2).
number N
pwd(). Prints current working directory pwd().
cd(Path). Changes directory cd("/home/user/project").
ls(). Lists files in current directory ls().
c(Module). Compiles and loads module c(math).
l(Module). Reloads compiled module l(math).
nl(Module). Reloads module from disk nl(math).
m(). Lists loaded modules m().
code:all_loaded(). Lists all loaded modules code:all_loaded().
code:is_loaded(Module). Checks if module is loaded code:is_loaded(math).
rr(Module). Loads record definitions from rr(user).
module
rr("file.hrl"). Loads records from header file rr("include/user.hrl").
b(). Shows all variable bindings b().
bt(Pid). Prints stack trace of process bt(Pid).
flush(). Displays and clears mailbox flush().
messages
self(). Returns current process ID self().
processes(). Lists all running processes processes().
process_info(Pid). Displays process info process_info(Pid).
i(). Lists process information summary i().
i(Pid). Shows detailed process info i(Pid).
memory(). Displays VM memory usage memory().
erlang:memory(). Detailed memory stats erlang:memory().
node(). Returns current node name node().
nodes(). Lists connected nodes nodes().
net_adm:ping(Node). Tests connection to another node net_adm:ping('node2@host').
application:which_applications().
Lists running applications application:which_applications().
observer:start(). Opens graphical system monitor observer:start().
timer:tc(Fun). Times execution of function timer:tc(fun() ->
lists:seq(1,1000) end).
spawn(Fun). Creates new process spawn(fun() -> io:format("Hi")
end).
2
, Command Description Example
Pid ! Msg Sends message to process Pid ! hello.
flush(). Reads received messages flush().
Ctrl + G Opens user switch command mode Press in shell
a (after Ctrl+G) Aborts current shell a
c (after Ctrl+G) Continue shell c
i (after Ctrl+G) Interrupt shell i
k (after Ctrl+G) Kill shell process k
q (after Ctrl+G) Quit Erlang VM q
2.Data Types in Erlang
Data in Erlang is usually referred to as terms ,built-in data types include: - Numbers (integers, floats) -
Binaries/Bitstrings - Atoms - Tuples - List(and strings) - Unique identifiers(pid, ports, reference) ###
2.1. Numbers and Arithmetic Erlang has two numerical data types integers and floats . Conversion is done
automatically by most of the arithmetic operations, so there is no need for any type coercion. Integers in
Erlang can be of arbitrary size, they can be written in any base between 2 and 36 (0-9. A-Z/a-z). Floats
are handled by 64-bit IEEE 754-1985 representation (double precision), There are no single precision
floating-point in Erlang.
Normal infix notation is used for common arithmetic operators:
+, -, *
for divisions 2 options exists:
%% yields a floating point number; ---> 2.0
%% div -> 7 div 2 ----> 3
The remainder of an integer division is given by the rem operator. There are some additional integer operators
for bitwise operations: N bsl K shifts the integer, N K steps to the left, and bsr performs a corresponding
arithmetic right shift. The bitwise logic operators are named band,bor,bxor, and bnot. For example,X
band (bnot Y) masks away those bits from X that are set in Y.
2.2.Binaries and bitstrings
In Erlang, binaries and bitstrings are low-level data types used for working with raw binary data efficiently —
things like network packets, file contents, protocol messages, cryptographic data, and serialization. Bitstrings
is a simple sequence of bits, this means it contains any number of bits not necessarily divisible by 8.
%1 = value and 3 = store it using 3 bits.
%001 will be the binary representation.
<<1:3>>
Binary is a bitstring whose size is a multiple of 8 bits (1 byte). Every binary is bitstring and not every
bitstring is a binary. Or a binary is – bitstring where bit_size mod 8 = 0 The relationship:
bitstring
-- binary
2.3.Atoms
Atoms are one of Erlang’s core data types. An atom is a constant whose value is its own name. Think
of them as symbolic labels or named constants. Atom are mainly used as identifiers. They are stored in the
table and referred to by the table index, and they have a length of 255 characters.
3
Vick Preston
Contents
Erlang Shell 1
2.Data Types in Erlang 3
2.2.Binaries and bitstrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.3.Atoms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.4.Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.5.Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.6.Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.7. Pids, ports, and references . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.8.Functions as data: funs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.9. Comparing Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.10 Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Core commands to remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.Modules and Functions 8
4. Variables and Pattern matching 9
6. Cases and Expressions 11
7. Funs 13
8.Exceptions, try, and catch 14
8.1 Throwing (raising) exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
9. Records 15
10. Preprocessing and include files 16
11. Processes 18
12. ETS Tables 19
13. Recursion - Function that calls itself 20
Erlang Shell
The Erlang shell is an interactive REPL(Read, Evaluate, Print, Loop). It enables execution of Erlang
expressions, test code, spawn processes, inspect state, compile modules, debug interactively. starts in :
erl
Erlang/OTP 28 [...]
Eshell V28
1
,1>
Everything in Erlang is an expression. The expressions are evaluated and values are returned.
1> 2 + 3.
5
The period matters. Erlang shell runs in two modes: - Normal mode ; which commands can be edited and
expressions evaluated - Job Control Mode (JCL); Jobs can be started, killed, detached or connected.
Erlang Shell Commands Cheat Sheet
Command Description Example
erl Starts the Erlang shell erl
q(). Quits the shell q().
init:stop(). Gracefully shuts down Erlang VM init:stop().
h(). Displays shell help h().
h(Module). Shows module help h(lists).
h(Module, Function). Shows help for specific function h(lists, map).
f(). Clears all variable bindings f().
f(Var). Clears specific variable binding f(X).
v(N). Retrieves result of shell command v(2).
number N
pwd(). Prints current working directory pwd().
cd(Path). Changes directory cd("/home/user/project").
ls(). Lists files in current directory ls().
c(Module). Compiles and loads module c(math).
l(Module). Reloads compiled module l(math).
nl(Module). Reloads module from disk nl(math).
m(). Lists loaded modules m().
code:all_loaded(). Lists all loaded modules code:all_loaded().
code:is_loaded(Module). Checks if module is loaded code:is_loaded(math).
rr(Module). Loads record definitions from rr(user).
module
rr("file.hrl"). Loads records from header file rr("include/user.hrl").
b(). Shows all variable bindings b().
bt(Pid). Prints stack trace of process bt(Pid).
flush(). Displays and clears mailbox flush().
messages
self(). Returns current process ID self().
processes(). Lists all running processes processes().
process_info(Pid). Displays process info process_info(Pid).
i(). Lists process information summary i().
i(Pid). Shows detailed process info i(Pid).
memory(). Displays VM memory usage memory().
erlang:memory(). Detailed memory stats erlang:memory().
node(). Returns current node name node().
nodes(). Lists connected nodes nodes().
net_adm:ping(Node). Tests connection to another node net_adm:ping('node2@host').
application:which_applications().
Lists running applications application:which_applications().
observer:start(). Opens graphical system monitor observer:start().
timer:tc(Fun). Times execution of function timer:tc(fun() ->
lists:seq(1,1000) end).
spawn(Fun). Creates new process spawn(fun() -> io:format("Hi")
end).
2
, Command Description Example
Pid ! Msg Sends message to process Pid ! hello.
flush(). Reads received messages flush().
Ctrl + G Opens user switch command mode Press in shell
a (after Ctrl+G) Aborts current shell a
c (after Ctrl+G) Continue shell c
i (after Ctrl+G) Interrupt shell i
k (after Ctrl+G) Kill shell process k
q (after Ctrl+G) Quit Erlang VM q
2.Data Types in Erlang
Data in Erlang is usually referred to as terms ,built-in data types include: - Numbers (integers, floats) -
Binaries/Bitstrings - Atoms - Tuples - List(and strings) - Unique identifiers(pid, ports, reference) ###
2.1. Numbers and Arithmetic Erlang has two numerical data types integers and floats . Conversion is done
automatically by most of the arithmetic operations, so there is no need for any type coercion. Integers in
Erlang can be of arbitrary size, they can be written in any base between 2 and 36 (0-9. A-Z/a-z). Floats
are handled by 64-bit IEEE 754-1985 representation (double precision), There are no single precision
floating-point in Erlang.
Normal infix notation is used for common arithmetic operators:
+, -, *
for divisions 2 options exists:
%% yields a floating point number; ---> 2.0
%% div -> 7 div 2 ----> 3
The remainder of an integer division is given by the rem operator. There are some additional integer operators
for bitwise operations: N bsl K shifts the integer, N K steps to the left, and bsr performs a corresponding
arithmetic right shift. The bitwise logic operators are named band,bor,bxor, and bnot. For example,X
band (bnot Y) masks away those bits from X that are set in Y.
2.2.Binaries and bitstrings
In Erlang, binaries and bitstrings are low-level data types used for working with raw binary data efficiently —
things like network packets, file contents, protocol messages, cryptographic data, and serialization. Bitstrings
is a simple sequence of bits, this means it contains any number of bits not necessarily divisible by 8.
%1 = value and 3 = store it using 3 bits.
%001 will be the binary representation.
<<1:3>>
Binary is a bitstring whose size is a multiple of 8 bits (1 byte). Every binary is bitstring and not every
bitstring is a binary. Or a binary is – bitstring where bit_size mod 8 = 0 The relationship:
bitstring
-- binary
2.3.Atoms
Atoms are one of Erlang’s core data types. An atom is a constant whose value is its own name. Think
of them as symbolic labels or named constants. Atom are mainly used as identifiers. They are stored in the
table and referred to by the table index, and they have a length of 255 characters.
3