Listas y cons en OCaml
Inmutables y enlazadas: :: construye, @ concatena, los patrones parten.
(* Lists are immutable and linked; :: builds, @ appends. *)
let morning = ["north"; "ledge"]
let all_pools = "cove" :: morning @ ["shelf"]
let () =
print_endline (String.concat " -> " all_pools);
Printf.printf "First: %s of %d\n"
(List.hd all_pools) (List.length all_pools);
(* head :: tail patterns split a list in a match. *)
match all_pools with
| first :: rest ->
Printf.printf "%s leads %d others\n" first (List.length rest)
| [] -> print_endline "no pools at all"
Cómo funciona
"cove" :: morning @ ["shelf"]construye sin copiar la cola.List.hdyList.lengthleen la forma.first :: restdesestructura cabeza y cola en un match.
Palabras clave y builtins usados aquí
letmatchwith
El intento, en números
- Líneas
- 13
- Caracteres a escribir
- 470
- Tokens
- 95
- Ritmo de tres estrellas
- 65 tpm
Al ritmo de tres estrellas de 65 tokens por minuto, este intento toma unos 88 segundos.
Paso 1 de 4 en Listas; paso 11 de 29 en Fundamentos del lenguaje.