typestar

Variables and atoms in Erlang

Uppercase names bind once; lowercase bare words mean themselves.

%% Variables start uppercase and bind once; atoms are bare names.
main(_) ->
    Operator = "Maud",
    Shift = night,
    io:format("~s works the ~p shift~n", [Operator, Shift]),

    %% A bound variable only matches its own value.
    Shift = night,
    io:format("Still the ~p shift~n", [Shift]),

    %% Atoms compare fast and mean themselves.
    io:format("Ready? ~p~n", [Shift =:= night]),
    io:format("Same as day? ~p~n", [Shift =:= day]).

How it works

  1. Operator binds on first use and never rebinds.
  2. Re-stating Shift = night is a match, not an assignment.
  3. =:= is exact equality; atoms compare in one step.

Keywords and builtins used here

The run, in numbers

Lines
13
Characters to type
413
Tokens
92
Three-star pace
65 tpm

At the three-star pace of 65 tokens a minute, this run takes about 85 seconds.

Type this snippet

Step 2 of 3 in Values, step 2 of 31 in Language basics.

← Previous Next →