Functions and pattern matching

A function in Erlang is a set of clauses, and a pattern decides which clause runs. This part takes clauses and patterns apart, adds the guard that narrows a clause, names the fields of a tuple with a record, and shares that definition through a header. The last lesson shows the macros and the attributes the compiler reads before it parses the code.

The same rules run in a function head, a case, a receive and a match in the middle of a function. A reader who can read one of them can read all five, which is why the part stays with one idea per lesson and repeats the shape in the examples.

4.1 Functions and clauses a function per clause, arity, the semicolon and the full stop Open 4.2 Pattern matching a pattern on the left, a value on the right, and the match that binds Open 4.3 Guards when, the guard expressions, and why a guard never has a side effect Open 4.4 Records a record definition, the field syntax, and the update of one field Open 4.5 Pattern matching in depth a match in a function head, a binary pattern, a map pattern and an as-pattern Open 4.6 Records in depth defaults, a nested record, and the choice between a record and a map Open 4.7 Macros and module attributes -define, -include, -ifdef, and the attributes the compiler reads Open

Lessons