typestar

zip and seq in Erlang

lists:seq counts, zip pairs, unzip splits back apart.

%% lists:zip pairs; lists:seq counts; unzip splits back apart.
main(_) ->
    Lines = lists:seq(1, 4),
    States = [idle, ringing, idle, fault],

    Board = lists:zip(Lines, States),
    io:format("Board: ~p~n", [Board]),

    [io:format("Line ~p: ~p~n", [L, S]) || {L, S} <- Board],

    {Nums, Sts} = lists:unzip(Board),
    io:format("Numbers again: ~p~n", [Nums]),
    io:format("Ringing count: ~p~n",
              [length([S || S <- Sts, S =:= ringing])]).

How it works

  1. lists:seq(1, 4) builds the range.
  2. zip pairs elements; lengths must agree.
  3. unzip recovers both lists from the pairs.

Keywords and builtins used here

The run, in numbers

Lines
14
Characters to type
418
Tokens
145
Three-star pace
60 tpm

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

Type this snippet

Step 3 of 4 in The lists module, step 13 of 31 in Language basics.

← Previous Next →