if in Erlang
All guards, tried in order; true is the else arm.
%% if in Erlang is all guards; true is the else arm.
grade(Calls) ->
if
Calls >= 100 -> heavy;
Calls >= 30 -> steady;
Calls > 0 -> light;
true -> silent
end.
main(_) ->
[io:format("~p calls: ~p traffic~n", [C, grade(C)])
|| C <- [140, 45, 3, 0]],
ok.
How it works
- Each arm is a guard expression, not a pattern.
- The first true guard's body answers the if.
true ->catches everything that fell through.
Keywords and builtins used here
endformatgradeifmain
The run, in numbers
- Lines
- 13
- Characters to type
- 251
- Tokens
- 83
- Three-star pace
- 65 tpm
At the three-star pace of 65 tokens a minute, this run takes about 77 seconds.
Step 4 of 4 in Pattern matching, step 7 of 31 in Language basics.