vault backup: 2023-10-02 14:45:43
This commit is contained in:
@ -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;;
|
||||
```
|
Reference in New Issue
Block a user