typestar

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

  1. Each arm is a guard expression, not a pattern.
  2. The first true guard's body answers the if.
  3. true -> catches everything that fell through.

Keywords and builtins used here

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.

Type this snippet

Step 4 of 4 in Pattern matching, step 7 of 31 in Language basics.

← Previous Next →