OCaml has built-in lists:
The list of integers: [1; 2; 3; 4; 5]; return: int list = [1; 2; 3; 4]
List of strings: ["this"; “what”, “other”] ;; return: string list = ["this"; "what"; "Other"]
Or you can use cons-cons :: to create lists:
1 :: 2 :: 3 :: [] ;; return: int list = [1; 2; 3]
To get the title (first element) of a list:
List.hd [1; 2; 3]
returns 1
To get the tail of the list (all items after the first item)
List.tl [1; 2; 3] return: int list = [2; 3]
In addition, you can see how the lists are implemented in the standard OCaml library by looking at:
[installation location for OCaml] /lib/ocaml/std-lib/list.ml
source share