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
lists:seq(1, 4)builds the range.zippairs elements; lengths must agree.unziprecovers both lists from the pairs.
Keywords and builtins used here
formatlengthmainsequnzipzip
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.
Step 3 of 4 in The lists module, step 13 of 31 in Language basics.