Erlang was built at Ericsson from 1986 by Joe Armstrong, Robert Virding and Mike Williams, for telephone exchanges. The requirements were unusual and they explain every design decision in the language: the system must handle enormous numbers of simultaneous, independent activities; it must never stop, including for upgrades; and a fault in one part must not damage the others.
Those requirements rule things out. Shared mutable memory is out, because one activity corrupting another's data is exactly the failure mode that must not exist. Operating system threads are out, because you cannot have two million of them. Stopping to deploy is out. What is left is isolated processes communicating by copying messages, which is the actor model, and it is the whole language.
The BEAM is the virtual machine Erlang runs on, and it is not a JVM-style VM with threads bolted on - concurrency is its purpose. A BEAM process is not an OS process or thread: it starts at around 300 words of memory, is created in microseconds, and a single machine comfortably runs millions. Each has its own heap, so garbage collection is per-process and never stops the world - a detail that matters enormously for latency, because a long GC pause in one process does not affect any other. The scheduler is pre-emptive, counting reductions (roughly, function calls) and switching after about 4,000, so a process cannot hog a core by looping. Phase 4 returns to all of this.
Installing. brew install erlang on macOS, apt install erlang on Debian-derived Linux, or the official packages from Erlang Solutions, which are more current. Add rebar3, the build tool, for anything larger than a single file.
The shell is where you will spend most of phase one. erl starts it. The things to know immediately: every expression ends with a full stop and a newline, and forgetting it leaves the shell waiting silently, which is the single most common first confusion. c(module). compiles and loads a file without leaving the shell. f(). forgets all variable bindings, which you need often because variables cannot be reassigned. Ctrl-G then q quits properly; Ctrl-C twice also works and is abrupt. And help(). lists the rest.
%% ----------------------------------------------------------------
%% In the shell. Note the full stop on EVERY expression - leave it
%% off and the shell sits there waiting, which is the usual first
%% five minutes of confusion.
%% ----------------------------------------------------------------
1> 2 + 3.
5
2> X = 10.
10
3> X = 11.
** exception error: no match of right hand side value 11
%% Not "cannot reassign". X = 11 is a PATTERN MATCH against a
%% bound variable, and 10 does not match 11. This is the single
%% most important idea in the language and it arrives in minute two.
4> f(X). % forget the binding
ok
5> X = 11. % now it binds again
11
6> f(). % forget everything
%% ----------------------------------------------------------------
%% A module. hello.erl
%% ----------------------------------------------------------------
-module(hello). % must match the filename
-export([world/0, add/2]). % Name/Arity. Nothing else is callable.
world() ->
io:format("Hello from the BEAM~n").
add(A, B) ->
A + B.
%% ----------------------------------------------------------------
%% Compiling, from the shell:
%%
%% 7> c(hello).
%% {ok,hello}
%% 8> hello:world().
%% Hello from the BEAM
%% ok
%% 9> hello:add(2, 3).
%% 5
%%
%% Or from a terminal:
%% erlc hello.erl -> hello.beam
%% erl -noshell -eval "hello:world(), halt()."
%% ----------------------------------------------------------------
%% Shell commands worth memorising on day one:
%%
%% c(Module). compile and load, without leaving the shell
%% f(). forget all bindings f(X). forget one
%% b(). show current bindings
%% rr("rec.hrl"). read record definitions into the shell
%% i(). every process on the node
%% q(). stop the node politely
%% Ctrl-G then q quit Ctrl-C twice quit, abruptly
%% help(). the rest
%% ----------------------------------------------------------------
%% A project, with rebar3:
%%
%% rebar3 new app myapp
%% cd myapp
%% rebar3 compile
%% rebar3 shell % compiles, starts a node, loads your app
%% rebar3 eunit
%% rebar3 release
%%
%% rebar3 shell is the loop you will live in: edit, then r3:compile()
%% at the shell prompt reloads without restarting the node.
%% ----------------------------------------------------------------
Discussion
No questions on this topic yet. Sign in to ask the first one.