vault backup: 2023-10-02 15:03:09
This commit is contained in:
@ -1,12 +1,12 @@
|
||||
## 1.1 - Product
|
||||
|
||||
```
|
||||
```Ocaml
|
||||
let rec product = function
|
||||
| [] -> 0
|
||||
| x::t -> x + product t;;
|
||||
| x::t -> x * product t;;
|
||||
```
|
||||
## 1.2 - Count
|
||||
```
|
||||
```Ocaml
|
||||
let rec count x n =
|
||||
if x = [] then
|
||||
0
|
||||
@ -19,7 +19,7 @@ let rec count x n =
|
||||
```
|
||||
|
||||
## 1.3 - Search
|
||||
```
|
||||
```Ocaml
|
||||
let rec search x n =
|
||||
if x = [] then
|
||||
0
|
||||
@ -32,7 +32,7 @@ let rec search x n =
|
||||
```
|
||||
|
||||
## 1.4 - $n^{th}$
|
||||
```
|
||||
```Ocaml
|
||||
let rec length l =
|
||||
if l = [] then
|
||||
0
|
||||
@ -46,8 +46,24 @@ let rec nth x n =
|
||||
Failwith "The list is too short"
|
||||
else
|
||||
let e::t = x in
|
||||
if e = x then
|
||||
1 + count t n
|
||||
if n = 0
|
||||
return e
|
||||
else
|
||||
count t n;;
|
||||
```
|
||||
nth t (n-1) ;;
|
||||
```
|
||||
|
||||
## 1.5 - Maximum
|
||||
```Ocaml
|
||||
let max l =
|
||||
if l = [] then
|
||||
0
|
||||
else
|
||||
let maxi = 0
|
||||
let rec m =
|
||||
let e::t = l
|
||||
if e >= maxi then
|
||||
maxi = e
|
||||
else
|
||||
m t
|
||||
```
|
||||
|
||||
|
@ -87,7 +87,7 @@ _: int list = [1 ; 2 ; 3; 4]
|
||||
|
||||
## 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
|
||||
Sequential traverse have to solution, first is the list is empty, then you stop. Else if the list is not empty, we extract the head and the tail, we treat the head and we traverse the trail (rec call)
|
||||
```Ocaml
|
||||
# let p = (1, 2);;
|
||||
val p: int*int = (1,2)
|
||||
|
Reference in New Issue
Block a user