Erlang from the ground up
Small modules, matching instead of assignment, and no loops in sight
- Beginner
- 12 chapters
- 207 min read
Learn Erlang by writing it: modules and main/1, atoms and numbers and text, the equals sign that matches, function heads and guards, case and if, lists walked by head and tail, tuples, maps with maps:get, the lists module, funs and higher-order functions, try and catch and throw, ending with a small sales report that folds, sorts and prints.
What You'll Learn
- Write a module that exports main/1 and prints with io:format
- Tell atoms, whole numbers, decimals and strings apart, and print each one
- Match a value against a pattern, unpack its shape, and test a bound name
- Write several clauses for one function and narrow them with guards
- Choose between case and if, and know that if needs a final true clause
- Walk a list with the empty case and the head and tail case
- Group fixed values in a tuple and read them by position or by pattern
- Store records in maps, read them with maps:get, and change one key safely
- Ask a list questions with map, filter, foldl, sum, sort and seq
- Hold a function in a value, pass it to another function and return one
- Handle a failing division and a thrown signal without stopping the program
- Assemble a report from maps, a fold, a sort and formatted money columns
Prerequisites
- No programming experience needed — this course starts at the first line
- Any other language you know will make the early chapters quick
Course Content
- 01Your first ErlangNot started
What an Erlang file actually is: a module, an exported entry point called main/1, and io:format writing the answers out.
- 02Values: atoms, numbers and textNot started
The handful of values Erlang has — atoms, whole numbers, decimals, strings and booleans — and the two different divisions and two different ways of printing text.
- 03Pattern matching: the equals sign that matchesNot started
The equals sign does not assign, it matches. How to bind a name, unpack a shape you already know, and pin a name to the value it holds.
- 04Function heads and guardsNot started
One function name can have several heads. Erlang picks the first one whose pattern matches and whose guards are true, which is how most decisions are really made.
- 05Decisions: case and ifNot started
The two decisions inside a function body: case, which matches a value against several patterns, and if, which is a ladder of guards with a required final true.
- 06Lists and recursionNot started
A list is a head and a tail. Two clauses — one for the empty list, one that takes the head and asks again about the tail — are how Erlang walks a list without a loop.
- 07Tuples: fixed groups of valuesNot started
A tuple groups a fixed number of values under one term, and is matched by position. Tagged tuples like {ok, Value} are how Erlang functions hand back an answer and its reason together.
- 08Maps: records with namesNot started
A map is a set of keys and values, read by name with maps:get. Updating one key writes a new map and leaves the old one as it was.
- 09The lists moduleNot started
map, filter, foldl, sum, sort and seq: the toolbox that does the walking for you, once you can read the two clauses it is built from.
- 10Funs and higher-order functionsNot started
A fun is a function you can hold in a variable, pass to another function and get back. Funs remember the values they were made with, which is what makes map and foldl expressive.
- 11Errors: try, catch and throwNot started
Errors in Erlang are values with a class. try runs code that might fail, catch says what to do about each class, and throw is a signal you can raise and catch yourself.
- 12Putting it together: a sales reportNot started
A capstone that uses most of the course: orders as maps, one foldl that aggregates them by item, a sorted list of rows, money rounded to two places, and a report with a footer.
