-
Notifications
You must be signed in to change notification settings - Fork 0
/
04_num.cl
214 lines (192 loc) · 2.76 KB
/
04_num.cl
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
;; Chapter 4
;; add1
;; ads 1 to a number
(defun add1 (x)
(+ x 1)
)
;; sub1
;; subtracts 1 from a number
(defun sub1 (x)
(- x 1)
)
;; zero
;; returns true if number is zero
(defun zero (x)
(cond
((eq 0 x) t)
(t nil)
)
)
;; o+
;; adds two numbers
(defun o+ (x y)
(cond
((zero y) x)
(t
(add1 (o+ x (sub1 y))))
)
)
;; o-
;; subtracts two numbers
(defun o- (x y)
(cond
((zero y) x)
(t
(sub1 (o- x (sub1 y))))
)
)
;; addtup
;; adds together a tup
(defun addtup (tup)
(cond
((null tup) 0)
(t
(o+ (car tup) (addtup (cdr tup))))
)
)
;; x
;; multiply two numbers
(defun x (a b)
(cond
((zero b) 0)
(t
(o+ a (x a (sub1 b))))
)
)
;; tup+
;; zip add two tups
(defun tup+ (tup1 tup2)
(cond
((null tup1) tup2)
((null tup2) tup1)
(t
(cons (o+ (car tup1) (car tup2))
(tup+ (cdr tup1) (cdr tup2))
)
)
)
)
;; gt
;; greater than
(defun gt (x y)
(cond
((zero x) nil)
((zero y) t)
(t
(gt (sub1 x) (sub1 y)))
)
)
;; lt
;; less than
(defun lt (x y)
(cond
((zero y) nil)
((zero x) t)
(t
(lt (sub1 x) (sub1 y)))
)
)
;; o=
;; equals
(defun o= (x y)
(cond
((gt x y) nil)
((lt x y) nil)
(t t)
)
)
;; up
;; exponential
(defun up (a b)
(cond
((zero b) 1)
(t
(x a (up a (sub1 b))))
)
)
;; divide
;; divide two numbers
(defun divide (x y)
(cond
((lt x y) 0)
(t
(add1 (divide (o- x y) y)))
)
)
;; _length
;; length of lat
(defun _length (lat)
(cond
((null lat) 0)
(t
(add1 (_length (cdr lat))))
)
)
;; pick
;; pick nth atom from lat
(defun pick (n lat)
(cond
((zero (sub1 n)) (car lat))
(t
(pick (sub1 n) (cdr lat)))
)
)
;; rempick
;; remove nth atom from lat
(defun rempick (n lat)
(cond
((o= n 1) (cdr lat))
(t
(cons (car lat)
(rempick (sub1 n) (cdr lat))
)
)
)
)
;; numberp
;; checks if element is numeric
;; no-nums
;; remove nums from lat
(defun no-nums (lat)
(cond
((null lat) '())
((numberp (car lat)) (no-nums (cdr lat)))
(t
(cons (car lat) (no-nums (cdr lat))))
)
)
;; all-nums
;; extract all nums from a lat
(defun all-nums (lat)
(cond
((null lat) '())
((numberp (car lat)) (cons (car lat) (all-nums (cdr lat))))
(t
(all-nums (cdr lat)))
)
)
;; equan?
;; check if two atom s are equal
(defun equan? (x y)
(cond
((and (numberp x) (numberp y)) (o= x y))
((or (numberp x) (numberp y)) nil)
(t
(eq x y))
)
)
;; occur
;; count occurances of atom in lat
(defun occur (x lat)
(cond
((null lat) 0)
((equan? x (car lat)) (add1 (occur x (cdr lat))))
(t
(occur x (cdr lat)))
)
)
;; one?
;; check if atom is one
(defun one? (x)
(o= x 1)
)