<center><img src="https://programmerhumor.io/wp-content/uploads/2023/05/programmerhumor-io-python-memes-programming-memes-6fef32a6e78e9b8-608x507.jpg" height=auto width=300/> </center>

## Linear list
### Def
A linear data structure is a list where the elements must be treated sequentially.
$$ \lambda = <e_1, e_2, e_3, ..., e_n> $$
> $e_1$ is the rank of the value e. In linear list, we start at 0 because we don't use index list. 

The linear list is evolutive, you can add or remove elements. Also, the linear list can be empty. 

### List in CAML
```Ocaml
# [1 ; 2 ; 3];;
_: int list = [1, 2, 3]
```
> In CAML, all the element in the list have to be of the same type. 

```Ocaml
# let l1 = ['H', 'i'];;
val l1 : char list = ['H'; 'i']
# let l2 = ['C'; 'a'; 'm'; 'l'];;
val l2 : char list = ['C' ; 'a' ;'m'; 'l']
# [l1; l2]
_: char list list : [['H'; 'i'] ; ['C' ; 'a' ;'m'; 'l']]
```
> The expression `char list` is the element type of the evaluation

```Ocaml
# ["H" ; 'i'];;
Error: string ≠ char
# [(function x -> x + 10); (function y -> 2*y)];;
_: (int -> int) list = [<fun> ; <fun>]
```

## The empty list
```Ocaml
# [];;
-: a list = []
```
## Recursive type
**`e::t` => adds the element e at the head (`e`) of the list tail (`f`)**

```Ocaml
# 1::[2; 3];;
_: int list = [1; 2; 3]

# 1::[];;
_: int list = [1]
```

**The constructor is right associative `e1::e2::e3::l` => `e1::(e2::(e3::l))`**
**Exemple :**
```Ocaml
# [1 ; 2; 3];;
_: int list = [1; 2; 3]
# 1::[2; 3];;
_: int list = [1; 2; 3]
# 1::2::[3];;
_: int list = [1; 2; 3]
# 1::2::3::[];;
_: int list = [1; 2; 3]
```

### Priority
```Ocaml
# 1 + 2::[];;
_: int list = [3]

# 1 < 2 :: [];;
Error: int ≠ int list
```
The priority is somewhere between the addition. To avoid any doubt in the code, we have to put parenthesis. 

## Concatenation
```Ocaml
# let l1 = [1; 2] and l2 = [3; 4];;
val l1: int list = [1, 2]
val l2: int list = [3, 4]

# l1::l2;;
Error: int list ≠ int

#l1 @ l2;;
_: int list = [1 ; 2 ; 3; 4]
```
> The operator `@`is not constant

## List manipulation
### Traverse
Sequential traverse have to solution, first is the list is empty, then you stop. Else if the list is not empty, we treat the head
```Ocaml
# let p = (1, 2);;
val p: int*int = (1,2)
# let (a, b) = p;;
val a: int = 1
val b: int = 2

# let l = [1; 2; 3];;
val l : int list = [1; 2; 3]
# let e::t = l;;
Warning: the pattern matching is not exhausitve
val e: int = 1
val t: int list = [2; 3]
# let rec length l = 
	if l = [] then 0
	else
		let e::t = l in
			1 +. length t;;
Warning : pattern matching not exhaustive
val length: 'a list -> int = <fun>
