-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnat.ml
72 lines (56 loc) · 1.35 KB
/
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
58
59
60
61
62
63
64
65
66
67
68
69
70
module type Nat =
sig
type nat
val compare : nat -> nat -> int
val of_int : int -> nat
val to_int : nat -> int
val to_string : nat -> string
val add : nat -> nat -> nat
val sub : nat -> nat -> nat
end
module Nat =
struct
type nat = Null | Suc of nat
let rec compare a b =
match a, b with
| Null , Null -> 0
| Null , Suc _ -> -1
| Suc _, Null -> 1
| Suc a, Suc b -> compare a b
let to_int a =
let rec iter buffer =
function
| Null -> buffer
| Suc a -> iter (buffer + 1) a in
iter 0 a
let to_string a =
to_int a |> string_of_int
let of_int i =
let rec iter buffer i =
if i > 0
then iter (Suc buffer) (i - 1)
else buffer in
iter Null i
let rec add a b =
match a, b with
| Null , b -> b
| Suc a, b -> Suc (add a b)
let rec sub a b =
match a, b with
| Null , _ -> Null
| a , Null -> a
| Suc a, Suc b -> sub a b
end
open Nat
let () =
of_int 123
|> to_string
|> print_endline;
compare (of_int 123) (of_int 456)
|> print_int |> print_newline;
add (of_int 123) (of_int 456)
|> to_string |> print_endline;
sub (of_int 123) (of_int 456)
|> to_string |> print_endline;
sub (of_int 456) (of_int 123)
|> to_string |> print_endline