-
Notifications
You must be signed in to change notification settings - Fork 0
/
milho_core.ml
133 lines (117 loc) · 3.01 KB
/
milho_core.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
exception Invalid_argument of string
let sum args =
let sum a b =
match a, b with
| Value.Number (an, ad), Value.Number (bn, bd) ->
Value.Number ((an * bd) + (bn *ad), ad * bd)
| _ -> Value.Nil
in
List.fold_left sum (Number (0, 1)) args
let sub args =
let negative x =
match x with
| Value.Number (a, b) -> Value.Number (-a, b)
| _ -> Value.Nil
in
let sub a b =
match a with
| Value.Nil -> b
| Value.Number _ -> sum [a; (negative b)]
| _ -> Value.Nil
in
List.fold_left sub Value.Nil args
let mul args =
let mul a b =
match a, b with
| Value.Number (an, ad), Value.Number (bn, bd) ->
Value.Number (an * bn, ad * bd)
| _ -> Value.Nil
in
List.fold_left mul (Number (1, 1)) args
let div args =
let inverse x =
match x with
| Value.Number (a, b) -> Value.Number (b, a)
| _ -> Value.Nil
in
let div a b =
match a with
| Value.Nil -> b
| Value.Number _ -> mul [a; (inverse b)]
| _ -> Value.Nil
in
List.fold_left div Value.Nil args
let abs args =
match args with
| Value.Number (n, d) :: [] ->
Value.Number (Stdlib.abs n, Stdlib.abs d)
| _ -> raise (Invalid_argument "Milho_core.abs")
let cmp args =
match args with
| a :: b :: _ ->
let a = abs [a] in
let b = abs [b] in
sub [a; b]
| _ -> Value.Nil
let less_than args =
match cmp args with
| Value.Number (n, _) ->
Value.of_boolean (n < 0)
| _ -> raise (Invalid_argument "Milho_core.less_than")
let greater_than args =
match cmp args with
| Value.Number (n, _) ->
Value.of_boolean (n > 0)
| _ -> raise (Invalid_argument "Milho_core.less_than")
let equal args =
match args with
| a :: b :: _ -> (
match a, b with
| Value.Symbol a, Value.Symbol b ->
Value.of_boolean (a == b)
| Value.String a, Value.String b ->
Value.of_boolean (a == b)
| Value.True, Value.True ->
Value.True
| Value.False, Value.False ->
Value.True
| Value.Nil, Value.Nil ->
Value.True
| Value.Number _, Value.Number _ ->
Value.of_boolean (Value.to_number a = Value.to_number b)
| _ ->
Value.False
)
| _ -> raise (Invalid_argument "Milho_core.equal")
let str args =
args
|> List.map Value.to_string
|> String.concat " "
let println args =
args
|> str
|> print_endline;
Value.Nil
let rec cons args =
match args with
| [] -> Value.List []
| value :: [] -> Value.List [value]
| value :: lst :: [] -> (
match lst with
| Value.List lst -> Value.List (value :: lst)
| v -> Value.List [value; v]
)
| value :: rest ->
cons [value; (cons rest)]
let car args =
match args with
| Value.List (value :: _) :: [] -> value
| _ -> raise (Invalid_argument "Milho_core.car")
let cdr args =
match args with
| Value.List (_ :: tl ) :: [] -> Value.List tl
| _ -> raise (Invalid_argument "Milho_core.cdr")
let length args =
match args with
| Value.List lst :: [] -> Value.Number (List.length lst, 1)
| _ -> raise (Invalid_argument "Milho_core.length")