What is Erlang and where it runs

Erlang is a functional programming language built for programs that must keep running. Joe Armstrong, Robert Virding and Mike Williams wrote it at Ericsson in 1986 for telephone switches, equipment that could not be taken down for an upgrade, and the language was released as open source in 1998. Its ideas are old and narrow on purpose: data never changes, a process shares nothing with another process, and a process that fails takes only itself down.

What Erlang is

A value in Erlang is called a term, and the number, the atom, the list, the tuple, the map and the function are all terms. A module is a file of functions, a function is a set of clauses that pattern match their arguments, and an expression ends with a full stop. There is no class, no object and no loop: repetition is a function that calls itself.

Variables are bound once. The equals sign is not an assignment but a match: the left side is a pattern, the right side is the value, and a variable that already holds a value must be matched with the same one. That single rule is what makes a list safe to send to another process: nobody can change it after it was sent.

A first program

The whole program is one file, greet.erl. The first line names the module and must match the file name, the second line lists the functions other modules may call, and main/0 is the function this page starts:

greet.erl

-module(greet). -export([main/0]). main() -> io:format("Hello from Erlang~n"), io:format("OTP release: ~s~n", [erlang:system_info(otp_release)]).

erlc compiles the file into greet.beam, and erl starts a node that loads it and calls the function. The node stops at halt(), which keeps the command usable in a script:

erlc greet.erl

erl -noshell -eval 'greet:main(), halt().'

Hello from Erlang OTP release: 27

~n is a newline and ~s is a string in the format of io:format; the lesson on io:format and output lists the other directives. The compiler, the beam file and the code path are the subject of Your first Erlang script.

Processes, not shared state

A running Erlang system is a group of processes. A process is much cheaper than an operating system thread, it has a mailbox, and it talks to the rest of the system by sending messages. spawn starts one, ! sends a message to it, and receive waits for a message that matches a pattern. This program starts an echo process, asks it a question and prints the answer:

ping.erl

-module(ping). -export([main/0]). %% A process that answers every message it receives. echo() -> receive {ping, From, Text} -> From ! {pong, Text}, echo() end. main() -> Echo = spawn(fun echo/0), Echo ! {ping, self(), "are you there?"}, receive {pong, Text} -> io:format("echo answered: ~s~n", [Text]) after 1000 -> io:format("no answer~n") end.

erlc ping.erl

erl -noshell -eval 'ping:main(), halt().'

echo answered: are you there?

spawn(fun echo/0) returns a pid, and self() is the pid of the process that runs main/0. The tuple {ping, From, Text} is one message: the echo process matches it, sends {pong, Text} back to the pid it was given, and waits for the next one. Nothing is shared between the two processes; the message carries a copy of the term. The after 1000 branch is a one second timeout, so a missing answer prints a line instead of hanging forever. Processes have their own lesson, Processes and message passing.

Where Erlang runs

The compiler writes bytecode for BEAM, the virtual machine of the Erlang runtime system, erts. BEAM and the libraries around it are packaged as OTP, the Open Telecom Platform: the standard library, the gen_server and supervisor behaviours, the database and the network modules. The language, the machine and the libraries ship together. This page was checked with OTP 27.

Erlang runs on Linux, Windows and macOS. Elixir compiles to the same bytecode and uses the same runtime, so the two languages meet in one node. The systems built with Erlang answer a lot of connections at once: WhatsApp used it for its messaging servers, RabbitMQ is an Erlang program, CouchDB stores documents in Erlang, and telecom equipment still runs it.

Why learn Erlang

Erlang teaches habits that travel to other languages: data that never changes, small functions that pattern match, and a failure that stays local. The runtime also makes the hard part visible: a link, a monitor, a supervision tree and a hot code upgrade are ordinary library calls, and the course reaches them without a framework in the way.

The syntax takes an afternoon. The model takes longer, because most languages teach the opposite: shared memory, a loop that changes a variable, and an exception that unwinds the whole call stack. The next lesson installs the runtime, and the one after it starts the shell.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which company created Erlang?

    Answer

    Ericsson

    Erlang grew out of Ericsson's need for telephone switches that survive software bugs without a reboot.

  2. What is a term in Erlang?

    Answer

    Any piece of data the program works with

    Numbers, atoms, lists, tuples, maps and functions are all terms: the language has one data universe.

  3. What is BEAM?

    Answer

    The virtual machine that runs Erlang programs

    BEAM is the virtual machine of the Erlang runtime: it loads the compiled code and runs the processes.

  4. What does OTP stand for?

    Answer

    Open Telecom Platform

    OTP, the Open Telecom Platform, is Erlang plus its standard libraries and design patterns.

  5. How do two Erlang processes share state?

    Answer

    They do not: they send each other messages

    Processes share nothing; a message is copied to the receiver, so no two processes write the same value.

  6. What does this program print? -module(q1). -export([main/0]). main() -> io:format("~p~n", [2 + 3 * 4]).

    Answer

    14

    Multiplication binds tighter than addition, so the value is 2 + 12, which is 14.

  7. What does this program print? -module(q2). -export([main/0]). main() -> io:format("~p~n", [lists:seq(1, 3)]).

    Answer

    [1,2,3]

    ~p prints a list without spaces between the elements, so the output is [1,2,3].

  8. What does this program print? -module(q3). -export([main/0]). main() -> Ping = spawn(fun() -> receive stop -> ok end end), Ping ! stop, io:format("sent~n").

    Answer

    sent

    The spawned process receives stop and ends, while main/0 prints sent and returns.

  9. What does this program print? -module(q4). -export([main/0]). main() -> X = 1, {X, Y} = {1, 2}, io:format("~p~n", [Y]).

    Answer

    2

    X matches 1 and binds Y to 2, so the value printed is 2.

  10. A variable already bound to 1 is matched against 2. What happens?

    Answer

    The match fails and the process dies with a badmatch error

    A bound variable is a pattern, not a place to store a new value; 1 does not match 2, and the process crashes with {badmatch,2}.

The examples on this page were compiled and run with Erlang/OTP 27 (erts-15.2.7.14) on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev