vault backup: 2023-10-06 14:47:45
This commit is contained in:
parent
fd901fb644
commit
890ea1b07b
@ -44,23 +44,17 @@ val search 'a -> 'a list -> bool = <fun>
|
|||||||
|
|
||||||
## 1.4 - $n^{th}$
|
## 1.4 - $n^{th}$
|
||||||
```Ocaml
|
```Ocaml
|
||||||
# let rec length l =
|
# let nth l n =
|
||||||
if l = [] then
|
if n <= 0 then
|
||||||
0
|
invalid_arg "n <= 0"
|
||||||
else
|
else
|
||||||
let e::t = l in
|
let rec nthrec = function
|
||||||
1 + length t;;
|
| ([],_) -> failwith "list too short"
|
||||||
# let rec nth x n =
|
| (h::_,1)-> h
|
||||||
if (x = [] || n =< 0) then
|
| (_::t, n) -> nthrec(t, n-1)
|
||||||
Invalid_arg "The number of element in the list is 0"
|
in
|
||||||
else if length x < n then
|
ntrec(l, n);;
|
||||||
Failwith "The list is too short"
|
val nth 'a list -> 'a -> int = <fun>
|
||||||
else
|
|
||||||
let e::t = x in
|
|
||||||
if n = 0
|
|
||||||
return e
|
|
||||||
else
|
|
||||||
nth t (n-1) ;;
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user