vault backup: 2023-10-25 16:58:43
This commit is contained in:
32
Algo/Courses/Chapter 7 - High Order (exercises).md
Normal file
32
Algo/Courses/Chapter 7 - High Order (exercises).md
Normal file
@ -0,0 +1,32 @@
|
||||
## Ex 1.2
|
||||
```Ocaml
|
||||
# let sum n =
|
||||
if n < 0 then
|
||||
invalid_arg "n<0"
|
||||
else
|
||||
let rec sumrc n =
|
||||
if n=0 then
|
||||
0
|
||||
else
|
||||
n + sum(n-1)
|
||||
in sumrc n;;
|
||||
val sum : int -> int = <fun>
|
||||
|
||||
(*Correction*)
|
||||
let sigma f n =
|
||||
if n<0 then
|
||||
invalid_arg "n<0"
|
||||
else
|
||||
let rec sig = function
|
||||
| 0 -> f0
|
||||
| n -> fn + sig (n-1)
|
||||
in sig n;;
|
||||
val sigma: (int -> int) -> int -> int = <fun>
|
||||
```
|
||||
|
||||
## Ex 2.1
|
||||
```
|
||||
# let map arg list =
|
||||
if list = [] then 0
|
||||
else
|
||||
let e::t = list in
|
Reference in New Issue
Block a user