-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnat.ml
57 lines (48 loc) · 937 Bytes
/
nat.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
52
53
54
55
56
57
type nat =
| Z
| S of nat
let rec add x y =
match x with
| Z -> y
| S x' -> S (add x' y)
let () =
let one = S Z in
let two = S one in
let three = S two in
let four = S three in
let five = S four in
assert(five = add two three)
let rec even x =
match x with
| Z -> true
| S Z -> false
| S S x' -> even x'
(* ou alors faire NOT du prédécesseur *)
let rec pred x =
match x with
| Z -> None
| S x' -> Some x'
let rec half x =
match x with
| Z -> Z
| S Z -> Z
| S S x' -> S (half x')
let () =
let one = S Z in
let two = S one in
let three = S two in
let four = S three in
let five = S four in
assert(two = half five)
let rec halfwa x =
match x with
| None -> None
| Some Z -> Some Z
| Some (S Z) -> None
| Some (S (S x')) -> (
match halfwa (Some x') with
| Some half_pred -> Some (S (half_pred))
| None -> None
)
let half_panic x =
halfwa (Some x)