typestar

Tuples and lists in Erlang

Fixed shapes in braces, growing chains in brackets.

%% Tuples group fixed shapes; lists grow and shrink.
main(_) ->
    Call = {call, 7, "5560199"},
    {call, Line, Number} = Call,
    io:format("Line ~p dials ~s~n", [Line, Number]),

    Queue = [Call, {call, 3, "5560142"}],
    Longer = [{busy, 9} | Queue],
    io:format("Waiting: ~p~n", [length(Longer)]),

    [First | Rest] = Longer,
    io:format("Next up: ~p~n", [First]),
    io:format("Behind it: ~p~n", [length(Rest)]).

How it works

  1. {call, Line, Number} destructures by matching the shape.
  2. [Head | Tail] builds and splits lists.
  3. length/1 walks the list; tuples know their size for free.

Keywords and builtins used here

The run, in numbers

Lines
13
Characters to type
394
Tokens
135
Three-star pace
65 tpm

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

Type this snippet

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

← Previous Next →