typestar

Language basics

31 steps in 9 sets of Erlang.

Erlang was built inside Ericsson in the 1980s to run telephone exchanges: systems that cannot stop, where failure is expected and handled rather than prevented. Its answers — millions of cheap processes sharing nothing, message passing as the only communication, supervisors restarting whatever dies, code swapped in a running system — became the blueprint every actor system since has copied. WhatsApp famously served hundreds of millions of users on it with a team of dozens.

This tour types the core through a telephone exchange, naturally: single-assignment variables and atoms, tuples and lists with head-tail matching, function heads and guards as the branching idiom, case and if, tail recursion with accumulators, list comprehensions, the lists module, funs, maps and records and proplists, strings against binaries, ok/error tuples, try/catch with its three error classes — and the heart of the language: spawn, !, receive with timeouts, the call-and-reply shape, and monitors that turn a process's death into a message. The Encore is the cross-language script trio in honest OTP-flavored Erlang.

Start this tour

Values

Pattern matching

  • Function-head matchingOne function, several clauses: the head does the branching.
  • Guardswhen refines a clause after its pattern matches.
  • caseMatch a value against patterns, in order, as an expression.
  • ifAll guards, tried in order; true is the else arm.

Recursion & comprehensions

  • RecursionThe loop is a function calling itself; accumulators keep it tail.
  • List comprehensionsMap and filter in one bracket; generators cross like a matrix.
  • Composing stagesNo pipe operator: nest the calls or name the stages.

The lists module

  • The lists modulemap, filter, foldl, sort: the daily kit.
  • Funsfun ... end makes a function value; clauses branch inside.
  • zip and seqlists:seq counts, zip pairs, unzip splits back apart.
  • Sortingsort takes a fun; keysort orders tagged tuples by position.

Data shapes

  • Maps=> writes, := updates and matches, maps:get reads with defaults.
  • RecordsA named tuple with compile-time field sugar and defaults.
  • ProplistsTagged tuples in a list: the classic options style.
  • Maps at scalemaps:fold, filter, and merge work the whole table at once.

Text & numbers

Errors as data

  • ok and error tuples{ok, Value} and {error, Reason}: the convention every API keeps.
  • try/catchThree error classes: throw, error, and exit, caught by name.

Processes

Encore

  • word_freq.erlA word-frequency table from lexemes, maps:update_with, one sort.
  • grade_report.erlReport cards from tagged tuples, guard-clause grading, ~4.1f.
  • temp_stats.erlA week of temperatures: a fold-picked extreme, bars, zip swings.

The other Erlang tours