vault backup: 2023-10-02 13:58:17
This commit is contained in:
parent
156b19d733
commit
1d85a712b0
11
.obsidian/workspace.json
vendored
11
.obsidian/workspace.json
vendored
@ -13,7 +13,7 @@
|
|||||||
"state": {
|
"state": {
|
||||||
"type": "markdown",
|
"type": "markdown",
|
||||||
"state": {
|
"state": {
|
||||||
"file": "Algo/Séminaire/Exercices seminaire.md",
|
"file": "Algo/Séminaire/Chapitre 6 - Lists.md",
|
||||||
"mode": "source",
|
"mode": "source",
|
||||||
"source": false
|
"source": false
|
||||||
}
|
}
|
||||||
@ -85,7 +85,7 @@
|
|||||||
"state": {
|
"state": {
|
||||||
"type": "backlink",
|
"type": "backlink",
|
||||||
"state": {
|
"state": {
|
||||||
"file": "Algo/Séminaire/Exercices seminaire.md",
|
"file": "Algo/Séminaire/Chapitre 6 - Lists.md",
|
||||||
"collapseAll": false,
|
"collapseAll": false,
|
||||||
"extraContext": false,
|
"extraContext": false,
|
||||||
"sortOrder": "alphabetical",
|
"sortOrder": "alphabetical",
|
||||||
@ -102,7 +102,7 @@
|
|||||||
"state": {
|
"state": {
|
||||||
"type": "outgoing-link",
|
"type": "outgoing-link",
|
||||||
"state": {
|
"state": {
|
||||||
"file": "Algo/Séminaire/Exercices seminaire.md",
|
"file": "Algo/Séminaire/Chapitre 6 - Lists.md",
|
||||||
"linksCollapsed": false,
|
"linksCollapsed": false,
|
||||||
"unlinkedCollapsed": true
|
"unlinkedCollapsed": true
|
||||||
}
|
}
|
||||||
@ -125,7 +125,7 @@
|
|||||||
"state": {
|
"state": {
|
||||||
"type": "outline",
|
"type": "outline",
|
||||||
"state": {
|
"state": {
|
||||||
"file": "Algo/Séminaire/Exercices seminaire.md"
|
"file": "Algo/Séminaire/Chapitre 6 - Lists.md"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -158,8 +158,9 @@
|
|||||||
},
|
},
|
||||||
"active": "88e153f7ea61ad97",
|
"active": "88e153f7ea61ad97",
|
||||||
"lastOpenFiles": [
|
"lastOpenFiles": [
|
||||||
"Algo/CM/CM du 27 septembre.md",
|
|
||||||
"Algo/Séminaire/Exercices seminaire.md",
|
"Algo/Séminaire/Exercices seminaire.md",
|
||||||
|
"Algo/Séminaire/Chapitre 6 - Lists.md",
|
||||||
|
"Algo/CM/CM du 27 septembre.md",
|
||||||
"Algo/Séminaire/Chapter 5 - Recursivity.md",
|
"Algo/Séminaire/Chapter 5 - Recursivity.md",
|
||||||
"COM-ADMR/Séjour international EPITA.md",
|
"COM-ADMR/Séjour international EPITA.md",
|
||||||
"Algo/CM",
|
"Algo/CM",
|
||||||
|
110
Algo/Séminaire/Chapitre 6 - Lists.md
Normal file
110
Algo/Séminaire/Chapitre 6 - Lists.md
Normal file
@ -0,0 +1,110 @@
|
|||||||
|
<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>
|
Loading…
x
Reference in New Issue
Block a user