Lists are an important data type in OCaml, providing a flexible interface for sequences. Unlike Go’s slices, OCaml uses immutable lists by default.
To run this program, save it as lists.ml and use the OCaml compiler:
Note that OCaml’s lists are immutable, so operations that seem to modify a list actually create a new list. The @ operator is used for list concatenation, and :: is used to add an element to the front of a list.
OCaml’s standard library provides many useful functions for working with lists, such as List.map, List.filter, and List.fold_left. These are powerful tools for list manipulation and are commonly used in functional programming.
In OCaml, multi-dimensional lists are represented as lists of lists. The inner lists can have varying lengths, providing flexibility similar to Go’s slices of slices.
Now that we’ve seen lists in OCaml, we’ll look at another key data structure: maps (which are called association lists or hash tables in OCaml, depending on the implementation).