From 1d85a712b09b5707fec89235ce17d3b16d22e9a9 Mon Sep 17 00:00:00 2001 From: Louis Date: Mon, 2 Oct 2023 13:58:17 +0200 Subject: [PATCH] vault backup: 2023-10-02 13:58:17 --- .obsidian/workspace.json | 11 +-- Algo/Séminaire/Chapitre 6 - Lists.md | 110 +++++++++++++++++++++++++++ 2 files changed, 116 insertions(+), 5 deletions(-) create mode 100644 Algo/Séminaire/Chapitre 6 - Lists.md diff --git a/.obsidian/workspace.json b/.obsidian/workspace.json index d33af51..c0eb312 100644 --- a/.obsidian/workspace.json +++ b/.obsidian/workspace.json @@ -13,7 +13,7 @@ "state": { "type": "markdown", "state": { - "file": "Algo/Séminaire/Exercices seminaire.md", + "file": "Algo/Séminaire/Chapitre 6 - Lists.md", "mode": "source", "source": false } @@ -85,7 +85,7 @@ "state": { "type": "backlink", "state": { - "file": "Algo/Séminaire/Exercices seminaire.md", + "file": "Algo/Séminaire/Chapitre 6 - Lists.md", "collapseAll": false, "extraContext": false, "sortOrder": "alphabetical", @@ -102,7 +102,7 @@ "state": { "type": "outgoing-link", "state": { - "file": "Algo/Séminaire/Exercices seminaire.md", + "file": "Algo/Séminaire/Chapitre 6 - Lists.md", "linksCollapsed": false, "unlinkedCollapsed": true } @@ -125,7 +125,7 @@ "state": { "type": "outline", "state": { - "file": "Algo/Séminaire/Exercices seminaire.md" + "file": "Algo/Séminaire/Chapitre 6 - Lists.md" } } }, @@ -158,8 +158,9 @@ }, "active": "88e153f7ea61ad97", "lastOpenFiles": [ - "Algo/CM/CM du 27 septembre.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", "COM-ADMR/Séjour international EPITA.md", "Algo/CM", diff --git a/Algo/Séminaire/Chapitre 6 - Lists.md b/Algo/Séminaire/Chapitre 6 - Lists.md new file mode 100644 index 0000000..cf3c9de --- /dev/null +++ b/Algo/Séminaire/Chapitre 6 - Lists.md @@ -0,0 +1,110 @@ +
+ +## Linear list +### Def +A linear data structure is a list where the elements must be treated sequentially. +$$ \lambda = $$ +> $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 = [ ; ] +``` + +## 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 =