-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.scm
236 lines (188 loc) · 5.42 KB
/
2.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
(load "util_math.scm")
(load "util.scm")
(define (linear-combination a b x y)
(+ (* a x) (* b y)))
; 2.1 Intro
(define (make-rational-basic n d) (cons n d))
(define (numer x) (car x))
(define (denom x) (cdr x))
(make-rational-basic 1 3)
(define (print-rat x)
(newline)
(display (numer x))
(display "/")
(display (denom x))
x)
(print-rat (make-rational-basic 1 3))
(define (make-rational-lowest-terms n d)
(let ((g (gcd n d)))
(cons (/ n g) (/ d g))))
(print-rat (make-rational-lowest-terms 20 4))
(define (add-rat x y)
(make-rational-lowest-terms (+ (* (numer x) (denom y)) (* (denom x) (numer y))) (* (denom x) (denom y))))
(print-rat (add-rat (make-rational-lowest-terms 1 3) (make-rational-lowest-terms 1 3)))
(print-rat (make-rational-basic -10 99))
; 2.1.3
(define (cons-y x y)
(define (dispatch m)
(cond ((= m 0) x)
((= m 1) y)
(else (error "Argument not 0 or 1 -- CONS" m))))
dispatch)
(define (car-y z) (z 0))
(define (cdr-y z) (z 1))
(define lol (cons-y "a" "b"))
(car-y lol)
(car-y lol)
(define (exercise-2.5)
(define (cons x y)
(* (expt 2 x) (expt 3 y)))
(define (not-integer? x y)
(not (integer? (/ x y))))
(define (base-prime x y null-value)
(if (= x null-value)
y
(base-prime (/ x null-value) (+ y 1) null-value)))
(define (car x)
(if (not-integer? x 3)
(base-prime x 1 2)
(car (/ x 3))))
(define (cdr x)
(if (not-integer? x 2)
(base-prime x 1 3)
(cdr (/ x 2))))
(newline)
(display "Car: ")
(display (car (cons 4 5)))
(newline)
(display "Cdr: ")
(display (cdr 3888))
(cons 4 5))
(exercise-2.5)
(define (exercise2.6)
(define (inc x)
(+ x 1))
(define zero (lambda (f) (lambda (x) x)))
(define one (lambda (f) (lambda (x) (f x))))
(define two (lambda (f) (lambda (x) (f (f x)))))
(newline)
(display ((one inc) 9))
(newline)
(display ((two inc) 9)))
(exercise2.6)
(define (prettier-print x y)
(newline)
(display x)
(display y)
(newline))
(define (exercise-2.1.4)
(define (make-interval a b)
(cons a b))
(define (upper-bound x)
(cdr x))
(define (lower-bound x)
(car x))
(define (sub-interval a b)
(make-interval (- (lower-bound a) (lower-bound b)) (- (upper-bound a) (upper-bound b))))
(define (interval-width x)
(/ (- (upper-bound x) (lower-bound x)) 2))
(prettier-print "Interval 10->20: " (interval-width (make-interval 10 20)))
(prettier-print "Interval 10->30: " (interval-width (make-interval 10 30)))
)
(exercise-2.1.4)
; 2.2.1 Closure Prop and Hierarchical Data
(define (basic-2.2.1)
(define one-through-four (list 1 2 3 4))
(define one-through (list 1))
(better-display (cdr one-through-four))
(better-display (car (cdr (cdr one-through-four))))
(better-display (car (cdr one-through-four)))
(better-display (cons 99 one-through-four)))
; (better-display (cdr one-through))
; (better-display (car (car one-through))))
(basic-2.2.1)
(define (list-operations-2.2.1)
(define (list-refs-i items n)
(if (= n 0)
(car items)
(list-refs-i (cdr items) (- n 1))))
(define (len-i items)
(define (iter itemz n)
(if (null? itemz)
n
(iter (cdr itemz) (+ n 1))))
(iter items 0))
(define (len-r items)
(if (null? items)
0
(+ 1 (len-r (cdr items)))))
(define (append-r list1 list2)
(if (null? list1)
list2
(cons (car list1) (append-r (cdr list1) list2))))
(define cubes (list 1 8 27 56 125))
(define odds (list 1 3 5 7 9 11 13))
(better-display (list-refs-i cubes 4))
(better-display (len-i cubes))
(better-display (append-r cubes odds))
0)
(list-operations-2.2.1)
(define (exercise-2.17)
(define (len-i items)
(define (iter itemz n)
(if (null? itemz)
n
(iter (cdr itemz) (+ n 1))))
(iter items 0))
(define items (list 1 2 3 4 5))
(define items-length (len-i items))
(define (last-pair it il)
(if (= il 1)
(car it)
(last-pair (cdr it) (- il 1))))
(better-display (last-pair items items-length)))
(exercise-2.17)
(define (exercise-2.18)
(define (reverse-list items)
(define (iter i l)
(if (null? i)
l
(iter (cdr i) (cons (car i) l))))
(iter (cdr items) (list (car items))))
(better-display (reverse-list (list 1 2 3 4 5 199 200))))
(exercise-2.18)
(define (exercise-2.20)
(define (z-ex1 a b . c)
(better-display c))
(define (z-ex2 . a)
(better-display (null? a)))
(z-ex1 1 2 3 4 5 6)
(z-ex2)
(z-ex2 1)
(define (len-i items)
(define (iter itemz n)
(if (null? itemz)
n
(iter (cdr itemz) (+ n 1))))
(iter items 0))
(define (same-parity . items)
(define il-odd? (odd? (len-i items)))
(define (iter i l)
; if il-odd? construct list with only odd numbers
; else if not il-odd? construct list with only even numbers
; if list is already empty, returned constructed list
(cond ((null? i)
l)
((equal? #t il-odd?)
(iter (cdr i) (if (even? (car i))
l
(cons (car i) l))))
((not il-odd?)
(iter (cdr i) (if (even? (car i))
(cons (car i) l)
l)))))
(iter items (list)))
(better-display (same-parity 1 2 3 4))
(better-display (same-parity))
(better-display (same-parity 1 2 3)))
(exercise-2.20)