diff --git a/.obsidian/workspace.json b/.obsidian/workspace.json index dca908b..745e8f7 100644 --- a/.obsidian/workspace.json +++ b/.obsidian/workspace.json @@ -18,21 +18,8 @@ "source": false } } - }, - { - "id": "accf3e9b23e83334", - "type": "leaf", - "state": { - "type": "markdown", - "state": { - "file": "Algo/Courses/Chapter 6 - Lists (Exercises).md", - "mode": "source", - "source": false - } - } } - ], - "currentTab": 1 + ] } ], "direction": "vertical" @@ -169,10 +156,11 @@ "command-palette:Open command palette": false } }, - "active": "accf3e9b23e83334", + "active": "88e153f7ea61ad97", "lastOpenFiles": [ - "Algo/Courses/Chapter 6 - Lists.md", + "Algo/Séminaire/Chapter 3 - Case analysis.md", "Algo/Courses/Chapter 6 - Lists (Exercises).md", + "Algo/Courses/Chapter 6 - Lists.md", "Algo/Courses", "Algo/Séminaire/Exercices seminaire.md", "Algo/CM/CM du 27 septembre.md", @@ -193,7 +181,6 @@ "COM-ADMR", "Methodologie", "Algo/Séminaire/Chapter 4 - A bit of imperative.md", - "Algo/Séminaire/Chapter 3 - Case analysis.md", "Algo/Séminaire/Introduction.md", "Algo/Séminaire/Chapter 1 - CAML basics.md", "Algo/Séminaire/assets/exception-meme.png", diff --git a/Algo/Courses/Chapter 6 - Lists (Exercises).md b/Algo/Courses/Chapter 6 - Lists (Exercises).md index d7eae56..7528ea6 100644 --- a/Algo/Courses/Chapter 6 - Lists (Exercises).md +++ b/Algo/Courses/Chapter 6 - Lists (Exercises).md @@ -11,9 +11,43 @@ let rec count x n = if x = [] then 0 else - let e::t = x + let e::t = x in if e = x then 1 + count t n else - count t n + count t n;; +``` + +## 1.3 - Search +``` +let rec search x n = + if x = [] then + 0 + else + let e::t = x in + if e = x then + true + else + search t n;; +``` + +## 1.4 - $n^{th}$ +``` +let rec length l = + if l = [] then + 0 + else + let e::t = l in + 1 + length t;; +let rec nth x n = + if (x = [] || n =< 0) then + Invalid_arg "The number of element in the list is 0" + else if length x < n then + Failwith "The list is too short" + else + let e::t = x in + if e = x then + 1 + count t n + else + count t n;; ``` \ No newline at end of file