summaryrefslogtreecommitdiffstats
path: root/ps03_evalapply/bnewbold_work.scm
blob: d968fd25adc1c2af4b9033c97acb86e8092bc6cb (plain)
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
;;; 6.945 Problem Set #3 Scheme Code
;;; 02/25/2009
;;; Bryan Newbold

; Note: See the attached bnewbold_ps3.txt for comments

; This file should be loaded into the generic-evaluation-environment

;;; Problem 3.1

(defhandler apply
  (lambda (proc-vector ops env)
    (vector-map
     (lambda (proc)
       (apply proc ops env))
     proc-vector))
  (lambda (thing)
    (and (vector? thing)
	 (not (compound-procedure? thing)))))

(define add1 (lambda (x) (+ 1 x)))

#| Test 
eval> (define cube (lambda (x) (* x x x)))
cube

eval> (cube 3)
27 

eval> ((vector cube sin cos sqrt) 1)
#(1 .8414709848078965 .5403023058681398 1)
|#

;;; Problem 3.2

(define ALLOW-SELF-EVALUATING-SYMBOLS #t)

(define (unbound-symbol? s)
  (if (symbol? s)
      (eq? 'unbound (environment-reference-type 
		     (nearest-repl/environment)
		     s))
      #f))

(define ((binary-pass symbol) a b)
  (if (not ALLOW-SELF-EVALUATING-SYMBOLS)
      (error "We don't ALLOW-SELF-EVALUATING-SYMBOLS!")
      (if (for-all? 
	      (list a b)
	    (lambda (x)
	      (or (number? x) 
		  (unbound-symbol? x)
		  (and (list? x) 
		       (not (null? x))
		       (or (member (car x) (list '+ '* '/ '-))
			   (literal-function? (car x))))))) ; see below
	  (list symbol a b)
	  (error "Not a number or symbol: " a b))))


(define add (make-generic-operator 2 (binary-pass '+)))
(define multiply (make-generic-operator 2 (binary-pass '*)))
(define divide (make-generic-operator 2 (binary-pass '/)))
(define subtract (make-generic-operator 2 (binary-pass '-)))

(defhandler add + number? number?)
(defhandler multiply * number? number?)
(defhandler divide / number? number?)
(defhandler subtract - number? number?)



#| Test:
(add (multiply 42 5365) (add (subtract 'a 3) (divide 4 5)))
;Value: (+ 225330 (+ (- a 3) 4/5))
(add (multiply 42 5365) (add (subtract 'a cos) (divide 4 5)))
;Not a number or symbol:  a #[compiled-procedure 69 ("arith" #xce) #xf #x1c05fb]
|#

(define *literal-functions* '())

(define (declare-literal-function f)
  (if (and (symbol? f) (< (string-length (symbol->string f)) 2))
      (begin
	(set! *literal-functions* (cons (environment-define
					 (nearest-repl/environment)
					 f
					 f)
					*literal-functions*))
	(display 
	 "Okey-doke, it is now a literal function"))
      (error "Not a good function name (make it short): " f)))

(define (literal-function? f)
  (if (member f *literal-functions*)
      #t
      #f))

(defhandler apply (lambda (f opts env)
		    (if ALLOW-SELF-EVALUATING-SYMBOLS
			(cons f (evaluate-list opts env))
			(error "We don't ALLOW-SELF-EVALUATING-SYMBOLS: " f)))
  literal-function?)

(defhandler eval (lambda (e env) e)
  literal-function?)

#| Test: TODO COPY
#|
eval> (declare-literal-function 'a)
Okey-doke, it is now a literal function#!unspecific

eval> a
a

eval> (a 2 4 5)
(a 2 4 5)

eval> (add (a 1 2) 'b)
(+ (a 1 2) b)

eval> (multiply (a (add 3 4) 'b) 99)
(* (a 7 b) 99)
|#
|#

;;; Problem 3.3

(define add-streams
  (lambda (a b)
    (kons (+ (car a) (car b))
	  (add-streams (cdr a) (cdr b)))))

(define ref-stream
  (lambda (s n)
    (if (= n 0)
	(car s)
	(ref-stream (cdr s) (- n 1)))))

(define (map-stream proc items)
  (kons (proc (car items))
	(map-stream proc (cdr items))))

(define (scale-stream items factor)
  (map-stream (lambda (x) (* x factor))
	      items))

#| type these in manually to the eval> prompt

(define (integral (integrand lazy) initial-value dt)
    (define int
    (kons initial-value
            (add-streams (scale-stream integrand dt)
                        int)))
    int)
; integral
(define (solve f y0 dt)
    (define y (integral dy y0 dt))
    (define dy (map-stream f y))
    y)
; solve
(ref-stream (solve (lambda (x) x) 1 0.001) 1000)
; 2.716923932235896

|#

;;; Problem 3.4a

(define some-options (list 'beef
			   'thai 'indian 'pasta 'sandwich
			   'crepes 'mexican)) ; len=7

#|
(define whats-for-dinner
  (kons (list-ref some-options (random-integer 7))
	whats-for-dinner))


(define (mealplan w)
  (pp (car w))
  (mealplan (cdr w)))

; Test: 

eval> (mealplan whats-for-dinner)
sandwich
sandwich
sandwich
sandwich
sandwich
sandwich
sandwich
sandwich
sandwich
sandwich
sandwich
sandwich
sandwich
sandwich
sandwich
sandwich
sandwich
sandwich
sandwich
sandwich
sandwich
sandwich
sandwich
sandwich
sandwich
sandwich
sandwich
sandwich
sandwich
sandwich
...etc

;That's depressing!

|#


;;; Problem 3.5

; Profiling!

(define PROFILING-ENABLED #f)

(define *primative-call-database*
  (make-eq-hash-table))

(define *compound-call-database*
  (make-eq-hash-table))

(define (print-profile-results)
  (set! PROFILING-ENABLED #f)
  (begin
    (display "----------- Profiling Results ---------------") (newline)
    (display "Primative Procedures:") (newline)
    (hash-table/for-each *primative-call-database*
			 (lambda (key val)
			   (display "   ")
			   (display val)
			   (display "    ")
			   (display key) (newline)))
    (display "Compound Procedures:") (newline)
    (hash-table/for-each *compound-call-database*
			 (lambda (key val)
			   (display "   ")
			   (display val)
			   (display "    ")
			   (display key) (newline)))
    (display "----------- End of Table ---------------") (newline)
    (set! PROFILING-ENABLED #t)))
 
(define ppr print-profile-results) ; shorter


(define (count-primative proc)
  (hash-table-set! *primative-call-database*
		   proc
		   (+ 1 (hash-table-ref/default *primative-call-database*
						proc
						0))))

(define (count-compound proc)
  (hash-table-set! *compound-call-database*
		   proc
		   (+ 1 (hash-table-ref/default *compound-call-database*
						proc
						0))))

; from rtdata:
(define strict-primative-procedure? procedure?)

(defhandler apply 
  (lambda (proc opers env)
    (if PROFILING-ENABLED (count-primative proc))
    (apply-primitive-procedure proc
			       (evaluate-list opers env)))
  strict-primative-procedure?)

(defhandler apply
  (lambda (procedure operands calling-environment)
    (if PROFILING-ENABLED (count-compound procedure))
    (if (not (= (length (procedure-parameters procedure))
		(length operands)))
	(error "Wrong number of operands supplied"))
    (let ((arguments
	   (map (lambda (parameter operand)
		  (evaluate-procedure-operand parameter
					      operand
					      calling-environment))
		(procedure-parameters procedure)
		operands)))
      (eval (procedure-body procedure)
	    (extend-environment
	     (map procedure-parameter-name
		  (procedure-parameters procedure))
	     arguments
	     (procedure-environment procedure)))))
  compound-procedure?)

#| Testing

;; the order is a little off here, I cherry picked out examples from a
;; session so the actual counts are off a bit

(init)
eval> (ppr)
----------- Profiling Results ---------------
Primative Procedures:
Compound Procedures:
----------- End of Table ---------------

eval> (define cube (lambda (x) (* x x x)))
cube

eval> (cube 12)
1728

eval> (ppr)
----------- Profiling Results ---------------
Primative Procedures:
   2    #[compound-procedure 18 print-profile-results]
   1    #[arity-dispatched-procedure 20]
   1    #[arity-dispatched-procedure 19]
Compound Procedures:
   1    #(compound-procedure (x) (* x x x) #((cube) (#(compound-procedure (x) (*
 x x x) #((cube) (#(compound-procedure (x) (* x x x) #((cube) (#(compound-proced
ure ... ... ...)) ()))) ()))) ()))
----------- End of Table ---------------

eval> (define (fib n) (if (< n 2) 1 (+ (fib (- n 2)) (fib (- n 1))))))
fib

eval> (fib 12)
233

eval> (ppr)
----------- Profiling Results ---------------
Primative Procedures:
   1    #[compound-procedure 21 operator]
   13134    #[arity-dispatched-procedure 19]
   1    #[compound-procedure 22 operator]
   1    #[arity-dispatched-procedure 20]
   26168    #[arity-dispatched-procedure 23]
   5    #[compound-procedure 18 print-profile-results]
   26173    #[arity-dispatched-procedure 24]
Compound Procedures:
   1    #(compound-procedure (x) (* x x x) #((fib cube) (#(compound-procedure (n) (if (< n 2) 1 (+ (fib (- n 2)) (fib (- n 1)))) #((fib cube) (#(compound-procedure (n) (if (< n 2) 1 (+ (fib ...) (fib ...))) #((fib cube) (#(compound-procedure ... ... ...) #(compound-procedure ... ... ...)) ())) #(compound-procedure (x) (* x x x) #((fib cube) (#(compound-procedure ... ... ...) #(compound-procedure ... ... ...)) ()))) ())) #(compound-procedure (x) (* x x x) #((fib cube) (#(compound-procedure (n) (if (< n 2) 1 (+ (fib ...) (fib ...))) #((fib cube) (#(compound-procedure ... ... ...) #(compound-procedure ... ... ...)) ())) #(compound-procedure (x) (* x x x) #((fib cube) (#(compound-procedure ... ... ...) #(compound-procedure ... ... ...)) ()))) ()))) ()))
   26172    #(compound-procedure (n) (if (< n 2) 1 (+ (fib (- n 2)) (fib (- n 1)))) #((fib cube) (#(compound-procedure (n) (if (< n 2) 1 (+ (fib (- n 2)) (fib (- n 1)))) #((fib cube) (#(compound-procedure (n) (if (< n 2) 1 (+ (fib ...) (fib ...))) #((fib cube) (#(compound-procedure ... ... ...) #(compound-procedure ... ... ...)) ())) #(compound-procedure (x) (* x x x) #((fib cube) (#(compound-procedure ... ... ...) #(compound-procedure ... ... ...)) ()))) ())) #(compound-procedure (x) (* x x x) #((fib cube) (#(compound-procedure (n) (if (< n 2) 1 (+ (fib ...) (fib ...))) #((fib cube) (#(compound-procedure ... ... ...) #(compound-procedure ... ... ...)) ())) #(compound-procedure (x) (* x x x) #((fib cube) (#(compound-procedure ... ... ...) #(compound-procedure ... ... ...)) ()))) ()))) ()))
----------- End of Table ---------------

|#