-
Notifications
You must be signed in to change notification settings - Fork 0
/
4.scm
247 lines (193 loc) · 4.13 KB
/
4.scm
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
;;; -*- buffer-read-only:t -*-
(load "3.scm")
(define (add1 x)
(+ x 1))
(define (sub1 x)
(- x 1))
;;; (add1 10)
;;; (sub1 10)
(define (o+ x y)
"add 2 numbers"
(cond ((zero? y) x)
(else (o+ (add1 x)
(sub1 y)))))
;;; (o+ 10 20)
(define (o+ x y)
"other variant"
(cond ((zero? y) x)
(else (add1 (o+ x
(sub1 y))))))
;;; (o+ 10 20)
(define (o- x y)
"subtract 2 numbers"
(cond ((zero? y) x)
(else (o- (sub1 x)
(sub1 y)))))
;;; (o- 10 10)
(define (o- x y)
"subtract 2 numbers"
(cond ((zero? y) x)
(else (sub1 (o- x
(sub1 y))))))
;;; (o- 10 9)
(define (addtup tup)
"add all numbers in a tuple"
(cond ((null? tup) 0)
(else (o+ (car tup)
(addtup (cdr tup))))))
;;; (addtup '(1 2 3 4))
(define (o* x y)
"multiply 2 numbers"
(cond ((zero? y) 0)
(else (o+ x
(o* x (sub1 y))))))
;;; (o* 12 3)
(define (tup+ tup1 tup2)
"build a new tuple with nth position = nth tup1 + nth tup2"
(cond ((and (null? tup1)
(null? tup2) '()))
((null? tup1)
(cons (car tup2)
(tup+ '()
(cdr tup2))))
((null? tup2)
(cons (car tup1)
(tup+ (cdr tup1)
'())))
(else (cons (o+ (car tup1)
(car tup2))
(tup+ (cdr tup1)
(cdr tup2))))))
;;; (tup+ '(2 3 3 10 0 0 1) '(4 6 0 10))
(define (tup+ tup1 tup2)
"other variant"
(cond ((and (null? tup1)
(null? tup2) '()))
((null? tup1)
tup2)
((null? tup2)
tup1)
(else (cons (o+ (car tup1)
(car tup2))
(tup+ (cdr tup1)
(cdr tup2))))))
;;; (tup+ '(1 2 3 4 5 6 7 8 9) '(4 6 0 10))
(define (o> x y)
"`x` is greater than `y` ?"
(cond ((zero? x) false)
((zero? y) true)
(else (o> (sub1 x)
(sub1 y)))))
;;; (o> 3 3)
(define (o< x y)
"`x` is smaller than `y` ?"
(cond ((zero? y) false)
((zero? x) true)
(else (o< (sub1 x)
(sub1 y)))))
;;; (o< 2 3)
(define (o= x y)
"equality predicate for numbers"
(cond ((and (zero? x)
(zero? y))
true)
((or (zero? y)
(zero? x))
false)
(else (o= (sub1 x)
(sub1 y)))))
;;; (o= 2 2)
(define (o= x y)
"other variant"
(cond ((zero? x) (zero? y))
((zero? y) false)
(else (o= (sub1 x)
(sub1 y)))))
;;; (o= 3 2)
(define (o= x y)
"other variant"
(cond ((or (o> x y)
(o< x y))
false)
(else true)))
;;; (o= 2 3)
(define (o^ x y)
"exponential"
(cond ((zero? y) 1)
(else (o* x
(o^ x
(sub1 y))))))
;;; (o^ 10 2)
(define (o/ x y)
"quotient of x/y"
(cond ((< x y) 0)
(else (add1 (o/ (o- x y)
y)))))
;;; (o/ 15 4)
(define (len lat)
"number of s-exp in a list"
(cond ((null? lat) 0)
(else (add1 (len (cdr lat))))))
;;; (len '(1 2 3 4 5 6))
(define (pick n lat)
"pick the nth number of a list"
(cond ((zero? (sub1 n)) (car lat))
(else (pick (sub1 n) (cdr lat)))))
;;; (pick 1 '( 0 1 2 3 4 5 6 ))
(define (rempick n lat)
"remove nth element from a list of s-exp"
(cond ((zero? (sub1 n)) (cdr lat))
(else (cons (car lat)
(rempick (sub1 n)
(cdr lat))))))
;;; (rempick 3 '(1 2 3 4))
(define (no-nums lat)
"remove all numbers from a list"
(cond ((null? lat) '())
((number? (car lat))
(no-nums (cdr lat)))
(else (cons (car lat)
(no-nums (cdr lat))))))
;;; (no-nums '(a b c 1 2 3 d))
(define (all-nums lat)
"extracts all numbers from a list and builds a tuple with them"
(cond ((null? lat) '())
((number? (car lat))
(cons (car lat)
(all-nums (cdr lat))))
(else (all-nums (cdr lat)))))
;;; (all-nums '(a 0 b c 1 2 3 d 4))
(define (eqan? a1 a2)
"equality for atoms and numbers"
(cond ((and (number? a1)
(number? a2))
(o= a1 a2))
((or (number? a1)
(number? a2))
false)
(else (eq? a1 a2))))
;;; (eqan? 3 2)
;;; (eqan? 'a 'x)
(define (occur a lat)
"number of occurrences of an atom in a list"
(cond ((null? lat)
0)
((eq? a (car lat))
(add1 (occur a (cdr lat))))
(else (occur a (cdr lat)))))
;;; (occur 'a '(x b c a a a y))
(define (one? n)
"equality with one"
(zero? (sub1 n)))
;;; (one? 10)
(define (one? n)
"other variant"
(= n 1))
;;; (one? 1)
(define (rempick n lat)
"remove nth element, other variant"
(cond ((one? n) (cdr lat))
(else (cons (car lat)
(rempick (sub1 n)
(cdr lat))))))
;;; (rempick 3 '(1 2 3 4))