typestar

Tuplas y listas en Erlang

Formas fijas entre llaves, cadenas crecientes entre corchetes.

%% 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)]).

Cómo funciona

  1. {call, Line, Number} desestructura matcheando la forma.
  2. [Head | Tail] construye y parte listas.
  3. length/1 camina la lista; las tuplas saben su tamaño gratis.

Palabras clave y builtins usados aquí

El intento, en números

Líneas
13
Caracteres a escribir
394
Tokens
135
Ritmo de tres estrellas
65 tpm

Al ritmo de tres estrellas de 65 tokens por minuto, este intento toma unos 125 segundos.

Escribe este fragmento

Paso 3 de 3 en Valores; paso 3 de 31 en Fundamentos del lenguaje.

← Anterior Siguiente →