# OCaml Tutorial: List, Array, Tuple

By Xah Lee. Date: . Last updated: .

## Lists

The syntax for list is

`[ element_1 ; element_2 ; element_3 ; … ]`

The last `;` is optional.

All elements must be of the same type.

```(* list syntax *)
(* elements must all be the same type *)
let x = [ 2; 8; 5; ];;```

### list length

```let x = [ 2; 8; 5; ];;
List.length x;;                 (* ⇒ 3 *)```

### list first, rest

```let x = [ 2; 8; 5; ];;

(* first element, called “head”. *)
List.hd x;;                             (* ⇒ 2 *)

(* rest elements, from 2nd to last, called “tail”. *)
List.tl x;;                             (* ⇒ [8; 5] *)```

The `List.hd` is a form of calling the “hd” function from the module “List”.

A list allows you to efficiently grow the list, by adding to or removing the first element of the list. However, accessing nth element takes time proportional to n.

### Prepend List

`x::list` → prepends x to the front of list `list`

```let x = [ 2; 8; 5; ];;

(* prepending a element *)
4::x;;                          (* ⇒ [4; 2; 8; 5; ] *)

(* prepending several elements *)
1::9::4::x;;                    (* ⇒ [1; 9; 4; 2; 8; 5] *)```

The syntax

`[a ; b ; c]`

is equivalent to

`a::b::c`

The operator `::` is right-associative. So, `a::b::c` means `a::(b::c)`

### Join Lists

2 lists can be joined by the operator `@`.

```(* join 2 lists *)
[ 2; 8; 5; ] @ [ 3; 4; ]```

The time to join 2 list is proportional to the number of items in first list.

## Array

Array is like a list of fixed length, and every element must be the same type.

The syntax for array is

`[| element_1 ; element_2 ; element_3 ; … |]`

```(* array syntax. Elements must be same type *)
let x = [| 2; 8; 3 |];;```

### array get

Here's array element access.

```(* array *)
let x = [| 2; 8; 3 |];;

(* getting a element *)
print_int x.(1);;         (* prints 8. Index starts at 0 *)```

### array set

Here's how to change a slot's value.

```(* array *)
let x = [| 2; 8; 3 |];;

(* changing array element *)
x.(1) <- 9;;             (* set 1st index element to 9 *)```

### create array

```(* creating array programmatically *)
(* first arg is num of items, second is init value *)
let x = Array.make 9 4;;```

The difference between list and array is that you cannot add or remove items in array. However, getting and setting arbitrary elements in array is fast, while getting/setting arbitrary elements in list is slow.

## n-tuples

n-tuple is like array but the elements need not be the same type.

```(* arbitrary n-tuple can be created *)
let x = 3,4,8 ;;                 (* 3-tuple *)
let y = 7.8, "abc", 4, 8;;      (* 4-tuple *)

(* when ambiguous, tuple needs to have parenthesis around *)```

### get tuple element, pattern matching

use pattern matching to access tuple element.

```(* pattern matching, to access tuple elements *)
let (a,b,c) = 3,4,7;;

print_int b;;                   (* 4 *)```

## Nesting List-Like Things

List, array, tuple can be nested arbitrarily, but remember, for list and array, their elements must all have the same type.

```(* nesting. Remeber, all elements must be same type. *)
let x = [ [1;2]; [3;4;5]; [6;7] ];;       (* list of lists *)
let x = [ [|1;2|]; [|3;4;5|]; [|6;7|] ];; (* list of arrays *)
let x = [ (1,2); (3,4); (5,6) ];;         (* list of tuples *)```
```(* nested array examples *)
let x = [| [|3; 4|]; [| 5; 6|] |] ;;    (* array of arrays *)
let x = [| [3; 4]; [ 5; 6] |] ;;        (* array of lists *)
let x = [| (3, "4"); (5, "7") |] ;;     (* array of tuples *)```

Note: when you have tuples as elements to list or array, each tuple must be the same type. That means, you cannot have tuples `(3,4)` and `(3.1, 4)` because the first is of type “int*int” and the second is “float*int”.

Note also the length of tuple must match. You cannot have elements `(3,4)` and `(3,4,5)` in a array or list. This is because, technically, `(3,4)` is of type “int*int” but `(3,4,5)` is of type “int*int*int”.

```[| (3 , "4"); (3. , "4") |] ;;    (* compiler error. *)
[| (3 , "4"); (3  , "4", 5) |] ;; (* compiler error. *)```
```(* a tuple, with elements being int, list, tuple, array *)
let x = (3, [4;5;6], (7,8), [| 9; 10 |]);;```

Tuple is the most flexible about types.