September 2013
Intermediate to advanced
548 pages
12h 25m
English
So far, we’ve used pattern matching for everything. This makes Erlang code small and consistent. But sometimes defining lots of separate function clauses is inconvenient. When this happens, we can use case or if expressions.
case has the following syntax:
| | case Expression of |
| | Pattern1 [when Guard1] -> Expr_seq1; |
| | Pattern2 [when Guard2] -> Expr_seq2; |
| | ... |
| | end |
case is evaluated as follows: First,
Expression is evaluated; assume this evaluates to
Value. Thereafter, Value is matched in turn
against Pattern1 (with the optional guard
Guard1), Pattern2, and so on, until a match is found. As soon as a match is found, then the corresponding expression sequence is evaluated—the result of evaluating ...
Read now
Unlock full access