Erlang is a functional, concurrent programming language designed for building robust, distributed, and fault-tolerant systems. It runs on the BEAM virtual machine, which is also used by Elixir.
What Makes Erlang Unique?
- Original BEAM Language: Erlang was the first language designed for the BEAM, and its concurrency, distribution, and fault-tolerance features are foundational (see “Concurrency and the BEAM” in the Elixir page).
- Lightweight Processes: Erlang processes are extremely lightweight and isolated, communicating only via message passing.
- Hot Code Swapping: Erlang supports updating code in a running system without downtime.
- “Let it Crash” Philosophy: Fault tolerance is achieved by isolating failures and using supervisors (see “Fault Tolerance” in the Elixir page).
Basic Syntax
Variables and Immutability
- Variables are immutable and must start with an uppercase letter.
X = 1,
X = 2. % Error: no rebinding allowedPattern Matching
- Used for assignment, function heads, and case expressions.
{A, B} = {1, 2}. % A = 1, B = 2
% Function with pattern matching
add({A, B}) -> A + B.Anonymous Functions
Add = fun(A, B) -> A + B end,
Add(1, 2). % 3No Pipe Operator
- Erlang does not have a pipe operator like Elixir’s |>, but function chaining can be done via nested calls.
Control Flow
if
true -> yes;
false -> no
end.
case {1, 2, 3} of
{4, 5, 6} -> no_match;
{1, X, 3} -> {matched, X}
end.Recursion (No Loops)
sum([]) -> 0;
sum([H|T]) -> H + sum(T).Concurrency and the BEAM
- See the “Concurrency and the BEAM” section in the Elixir page for a conceptual overview.
- Syntax for spawning processes and message passing in Erlang:
Pid = spawn(fun() -> io:format("Hello from a process~n") end).
Pid ! {hello, "Hi there!"}.
receive
{hello, Msg} -> io:format("~s~n", [Msg])
end.Metaprogramming
- Erlang does not have macros like Elixir, but supports parse transforms for advanced code manipulation (less common in everyday code).
Summary Table: Erlang vs. Elixir
| Feature | Erlang | Elixir |
|---|---|---|
| Syntax | Prolog-like, terse | Ruby-like, expressive |
| Variable Binding | Single assignment | Rebinding allowed |
| Pipe Operator | No | Yes (|>) |
| Macros | Parse transforms | Hygienic macros |
| Community | Telecom, infrastructure | Web, general purpose |
See Also
- For BEAM, concurrency, and fault-tolerance concepts, see the relevant sections in the Elixir page.
- For more on Functional Programmingbasics, see “Basic Syntax” in the Elixir page.