-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhw1.ml
52 lines (40 loc) · 870 Bytes
/
hw1.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
let rec iter(n,f)=
match (n,f) with
| (0,f) ->(fun x->x)
| (n,f) -> f iter(n-1,f)
let _ = print_int( iter(5,fun x->x+5) 3)
type nat = ZERO | SUCC of nat
let rec natadd (a,b) =
match(a,b) with
| (ZERO,ZERO) -> ZERO
| (ZERO, b) -> b
| (a, ZERO) -> a
| (SUCC a', SUCC b') -> SUCC(natadd(a,b'))
let rec merge (l1,l2) =
match (l1,l2) with
| ([], _) -> l2
| (_, []) -> l1
| (h1::t1, h2::t2) -> h1::h2::merge(t1,t2)
let rec natmul(a,b)=
match(a,b) with
| (ZERO,ZERO)->ZERO
| (ZERO,b)->ZERO
| (a,ZERO)->ZERO
| (SUCC a', SUCC b') -> natadd(natmul(a,b'),a)
(*
let rec printer msg =
match msg with
| [] -> ()
| h::t ->
print_int h;
printer t
*)
(*let greeter = merge([1;2;3],[4;5;6])
let _ = printer greeter
*)
let rec sigma(a,b,f) =
if a>b-1 then 0
else f(b) + sigma(a,b-1,f)
(*
let _ = print_int(sigma(0,5,fun x->x+3))
*)