-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.0.rkt
1252 lines (1060 loc) · 36 KB
/
3.0.rkt
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#lang sicp
(define balance 101)
(define (withdraw amount)
(if (> amount balance)
"Insufficient funds!"
(begin (set! balance (- balance amount)) ; set <name> <new-value>
balance)))
(withdraw 80)
(withdraw 500)
(define new-withdraw
(let ((balance 100))
(lambda (amount)
(if (>= balance amount)
(begin (set! balance
(- balance amount))
balance)
"Insufficient funds"))))
(new-withdraw 10)
(define (make-withdraw balance)
(lambda (amount)
(if (>= balance amount)
(begin (set! balance
(- balance amount))
balance)
"Insufficient funds")))
(define another-withdraw (make-withdraw 2000))
(another-withdraw 1004)
; test of define
; so whether bnew-withdraw is a function or a function-that-returns-a-function
; depends only on whether we do (define bnew-withdraw) or (define (bnew-withdraw))
(define bbalance 500)
(define (bnew-withdraw)
(lambda (amount)
(if (>= bbalance amount)
(begin (set! bbalance
(- bbalance amount))
bbalance)
"Insufficient funds")))
((bnew-withdraw) 50)
; deposits as well as withdrawals
(define (make-account balance)
(define (deposit amount)
(set! balance (+ balance amount))
balance)
(define (withdraw amount)
(set! balance (- balance amount))
balance)
(define (dispatch m)
(cond ((eq? m 'w) withdraw)
((eq? m 'd) deposit)
(else "unknown")))
dispatch)
(define acc (make-account 5000))
((acc 'd) 5000)
"Exercise 3.1: Accumulators"
(define (make-accumulator amount)
(define (accumulator addend)
(set! amount (+ amount addend))
amount)
accumulator)
(define A (make-accumulator 5))
(A 10) ; should be 15
(A 10) ; should be 25
"Exercise 3.2: Function call counting"
; Write a procedure make-monitored that takes as input a procedure,
; f, that itself takes one input.
(define (make-monitored f)
(let ((calls 0))
(define (inner arg)
(cond ((number? arg)
(begin (set! calls (+ calls 1))
(f arg)))
((eq? arg 'how-many-calls?)
calls)
((eq? arg 'reset-count)
(set! calls 0))
(else (error "no!"))))
inner))
; Test our function
(define (afunc arg)
(+ arg 999))
(define monitored-afunc (make-monitored afunc))
(monitored-afunc 6)
(monitored-afunc 2)
(monitored-afunc 4)
(= (monitored-afunc 'how-many-calls?) 3)
(define monitored-afunc2 (make-monitored afunc))
(monitored-afunc2 10)
(= (monitored-afunc2 'how-many-calls?) 1)
(monitored-afunc2 'reset-count)
(= (monitored-afunc2 'how-many-calls?) 0)
"Exercise 3.3 : password protection"
; modify make-account to accept a password
(define (make-account-2 balance password)
(define (deposit amount)
(set! balance (+ balance amount))
balance)
(define (withdraw amount)
(set! balance (- balance amount))
balance)
(define (dispatch m)
(cond ((eq? m 'w) withdraw)
((eq? m 'd) deposit)
(else "unknown")))
(define (wrong-password arg) ; a little ugly to have this unused arg
"Incorrect password")
(define (check-password-and-dispatch pwd message)
(if (eq? pwd password)
(dispatch message)
wrong-password))
check-password-and-dispatch)
(define acc-2
(make-account-2 100 'qwerty))
; try to withdraw 40 with correct password
(eq? ((acc-2 'qwerty 'w) 40) 60)
; try to withdraw 50 with incorrect password
(eq? ((acc-2 'some-other-password 'd) 50) "Incorrect password")
"Exercise 3.4: call-the-cops if the wrong password is used too often"
; The exercise says 7 but we'll do 3
(define (call-the-cops a)
"cops have been called")
(define (make-account-3 balance password)
(define tries 0)
(define (deposit amount)
(set! balance (+ balance amount))
balance)
(define (withdraw amount)
(set! balance (- balance amount))
balance)
(define (dispatch m)
(cond ((eq? m 'w) withdraw)
((eq? m 'd) deposit)
(else "unknown")))
(define (wrong-password arg) ; a little ugly to have this unused arg
"Incorrect password")
(define (check-password-and-dispatch pwd message)
(if (eq? pwd password)
(begin
(set! tries 0)
(dispatch message))
(begin
;(display tries)
(if (= tries 2)
call-the-cops
(begin
(set! tries (+ tries 1))
wrong-password)))))
check-password-and-dispatch)
(define acc-3
(make-account-3 100 'qwerty))
(eq? ((acc-3 'some-other-password 'd) 1) "Incorrect password")
(eq? ((acc-3 'some-other-password 'd) 1) "Incorrect password")
(eq? ((acc-3 'qwerty 'd) 1) 51)
(eq? ((acc-3 'some-other-password 'd) 1) "Incorrect password")
(eq? ((acc-3 'some-other-password 'd) 1) "Incorrect password")
(eq? ((acc-3 'some-other-password 'd) 1) "cops have been called")
; 3.1.2 Benefits of Assignment
"Exercise 3.5 : Monte Carlo Integration"
(define (monte-carlo trials experiment)
(define (iter trials-remaining trials-passed)
(cond ((= trials-remaining 0)
(/ trials-passed trials))
((experiment)
(iter (- trials-remaining 1)
(+ trials-passed 1)))
(else
(iter (- trials-remaining 1)
trials-passed))))
(iter trials 0))
(define (random-in-range low high)
(let ((range (- high low)))
(+ low (random range))))
(define (estimate-integral P x1 x2 y1 y2 trials)
(define (integral-test)
(P (random-in-range x1 x2)
(random-in-range y1 y2)))
(monte-carlo trials integral-test))
(define (Pcircle x y)
; Check that the distance to the centre is < the radius
(< (sqrt (+
(expt (abs (- x 1.0)) 2.0)
(expt (abs (- y 1.0)) 2.0)))
1))
(define (Prect x y)
(< x 1))
(Pcircle 1 1) ; should be #t
(Pcircle 1.5 1.5) ; should be #t
(not (Pcircle 2 2)) ; should be #f
(not (Pcircle 0 0)) ; should be #f
(define (estimate-pi trials)
; area = pi * r^2 so
; pi = area / r^2 and
; we are doing a unit circle so R^2 = 1 so
; pi = area
; note: a unit circle fits in a 2 x 2 box, not a 1 x 1 box
(* 4
; Remember to use floats throughout so that (random) returns floats.
(estimate-integral Pcircle 0.0 2.0 0.0 2.0 trials))
)
(estimate-pi 100000) ; 3.14170...
"Exercise 3.6 : A RNG that you can reset"
(define (make-new-rand)
(define x 44)
(lambda (msg)
(if (eq? msg 'generate)
(begin (set! x (modulo (+ (* 44 x)
9) 223456))
x)
(lambda (reset-to)
(set! x reset-to)))))
(define new-rand (make-new-rand))
(new-rand 'generate) ; a)
(new-rand 'generate)
(new-rand 'generate)
((new-rand 'reset) 44) ; Should be the same as a)
(new-rand 'generate)
"Exercise 3.7 : Joint accounts"
(define (make-joint account orig-pwd new-pwd)
(if (number? ((account orig-pwd 'd) 0))
; then orig-pwd is correct
(lambda (pwd action)
(if (eq? pwd new-pwd)
; pwd is correct
(account orig-pwd action)
"Incorrect pwd"
))
(lambda (b c)
"Incorrect orig-pwd")))
(define peter-acc
(make-account-3 100 'mynameispeter))
((peter-acc 'mynameispeter 'd) 20)
(define paul-acc (make-joint peter-acc
'mynameispeter
'mynameispaul))
(= 110 ((paul-acc 'mynameispaul 'w) 10))
(eq? (paul-acc 'mynamezzispeter 'w) "Incorrect pwd")
"Exercise 3.8 : Order of evaluation"
; define a simple procedure, f, such that (+ (f 0) (f 1)) will return
; 0 if the order of evaluation is left to right and 1 if right to left
(define (make-f x)
(lambda (a)
(if (< a x)
(begin (set! x a)
x)
x)))
(define f (make-f 1))
(+ (f 0) (f 1))
(define f2
(let ((called #f))
(lambda (x)
(if called
0
(begin
(set! called #t)
x)))))
(+ (f2 0) (f2 1))
f2
"3.9 Environments and Factorials"
; From 1.2.1, we have to methods to compute factorials
; Show the environment structures created by evaluating the fact of 6
; for both methods.
; recursive
(define (factorial n)
(if (= n 1)
1
(* n (factorial (- n 1)))))
; global env has factorial function that points to global env
; E1 created by call to (factorial 6) with n=6. E1 points to global env.
; E2 created by call to (factorial (- n 1)) with n=5. E2 points to global env.
; ...
; E6 created with n=1. E6 points to global env. Function returns 1.
; and iterative
(define (fact-iter product
counter
max-count)
(if (> counter max-count)
product
(fact-iter (* counter product)
(+ counter 1)
max-count)))
(define (fact2 n)
(fact-iter 1 1 n))
; global env has fact2 function that points to global env
; E1 created by call to (fact2 6) with n=5. E1 points to global env.
; E2 created by call to (fact-iter 1 1 6). E2 points to global env.
; E3 created by call to (fact-iter 1 2 6). E3 points to global env.
; ...
; E8 created by call to (fact-iter x 7 6). E8 points to global env.
; function returns x
"Exercise 3.10 Let and make-withdraw"
; One way of making make-withdraw
(define (make-withdraw-1 balance)
(lambda (amount)
(if (>= balance amount)
(begin (set! balance
(- balance amount))
balance)
"Insufficient funds")))
; An alternative
(define (make-withdraw-2 initial-amount)
(let ((balance initial-amount))
(lambda (amount)
(if (>= balance amount)
(begin (set! balance
(- balance amount))
balance)
"Insufficient funds"))))
; Recall that
; (let ((⟨var⟩ ⟨exp⟩)) ⟨body⟩)
; is interpreted as an alternate syntax for
; (lambda (⟨var⟩) ⟨body⟩) ⟨exp⟩)
; Illustrate how the two make-withdraw procedures above would handle
(define W1 (make-withdraw 100))
(W1 50)
(define W2 (make-withdraw 100))
; Show that the two versions create the same objects.
; How do the environment structures differ?
; 1
; -------------
; | global <------------
; | make-withdraw-1 ----> params: initial-amount, body..., env |
; | W1
; | W2
; -------------
"Exercise 3.12 Append!ing"
(define (last-pair x)
(if (null? (cdr x))
x
(last-pair (cdr x))))
(define (append! x y)
(set-cdr! (last-pair x) y)
x)
(define x (list 'a 'b))
(define y (list 'c 'd))
(define z (append x y))
;z
(cdr x)
(define w (append! x y))
;w
(cdr x)
"Exercise 3.13"
(define (make-cycle x)
(set-cdr! (last-pair x) x)
x)
; Q) Draw a diagram to show the structure of
(define zz (make-cycle (list 'a 'b 'c))) ; done on paper
; and explain what will happen if we were to run
;(last-pair zz)?
; A) It will never terminate
"Exercise 3.14: mysteries"
(define (mystery x)
(define (loop x y)
(if (null? x)
y
(let ((temp (cdr x)))
(set-cdr! x y)
(loop temp x))))
(loop x '()))
; Q) What does mystery do?
; A) It reverses a list
(define vv (list 'a 'b 'c 'd))
(define ww (mystery vv))
vv ; Note that the first call to loop does (set-cdr! '(a b c d) '())
ww
"Exercise 3.16: counting pairs"
(define (count-pairs x)
(if (not (pair? x))
0
(+ (count-pairs (car x))
(count-pairs (cdr x))
1)))
; Q) Why is this implementation wrong? Show inputs of 3 pairs for which
; it will return 3, 4, 7 or never return.
(count-pairs (list (list 1 2))) ; =3
(define pair-z (list 3)) ; 1 pair
(define pair-a (cons 2 pair-z)) ; 2 pairs -> (1 (2 null))
(define pair-b (cons pair-a pair-z)) ; now 3 pairs (pair-a pair-a)
(count-pairs pair-b) ; =4
;(count-pairs pair-a)
(define pair-3 pair-z)
(define pair-2 (cons pair-z pair-z))
(define pair-1 (cons pair-2 pair-2))
(count-pairs pair-1) ;= 7
(define pair-i (list 3)) ; 3,null
(define pair-j (cons 2 pair-i)) ;2, pair-i
(define pair-k (cons 1 pair-j)) ; 1, pair-j
(count-pairs pair-k)
(set-cdr! pair-i pair-k) ; the end of pair-i now wraps around to pair-k
;(count-pairs pair-k) ; will never return
"Exercise 3.17 : A correct version of count-pairs"
; Hint: maintain a list of visited pairs
(define (count-pairs-2 x)
(define (already-visited? a b)
; is a in b?
(if (null? b)
#f
(if (eq? a (car b))
#t
(already-visited? a (cdr b)))))
(define visited (list 999))
(define (count-pairs-visited y)
(if (not (pair? y))
0
(let ((been-here (already-visited? y visited)))
(append! visited (list y)) ; changes visited
(if been-here
0
(+ (count-pairs-visited (car y))
(count-pairs-visited (cdr y))
1))
)))
(count-pairs-visited x))
; These all really have 3 pairs in them
(= 3 (count-pairs-2 pair-k))
(= 3 (count-pairs-2 pair-1))
(= 3 (count-pairs-2 pair-b))
"Exercise 3.18 : Contains-cycle"
(define (cycles? a_list)
(define (already-visited? a b)
; is a in b?
(if (null? b)
#f
(if (eq? a (car b))
#t
(already-visited? a (cdr b)))))
(define visited (list))
(define (inner-cycles? a_list)
(if (null? a_list)
#f
(if (already-visited? a_list visited)
#t
(begin (set! visited (cons a_list visited))
(inner-cycles? (cdr a_list))))))
(inner-cycles? a_list))
(cycles? (list 1))
(define list_cycles (list 1 2 3))
(make-cycle list_cycles)
(cycles? list_cycles)
"Exercise 3.19 : Constant-space contains-cycle"
; Above, we make use of the "visited" list, which adds linear memory.
(define (make-cheeky-cycle a_list)
; makes a list with a cycle on the last element
; from 1,-> 2,-> 3,()
; to 1,-> 2,-> 3,|
; ^--
(if (null? (cdr a_list))
(set-cdr! a_list a_list)
(make-cheeky-cycle (cdr a_list))))
(define list_cheeky_cycles (list 1 2 3))
(make-cheeky-cycle list_cheeky_cycles)
list_cheeky_cycles
(cycles? list_cheeky_cycles)
(define (cs-cycles? a_list)
; constant-space cycles
; a_list: a list that may, or may not, cycle
; returns: #t or #f
(define (in-list? the-list stop-at item)
; go step through the-list
; if we encounter item, return #t
; if we get to stop-at, return #f
(cond ((eq? the-list item) #t)
((eq? the-list stop-at) #f)
(else (in-list? (cdr the-list) stop-at item))))
(define (inner-cycles? c_list)
(cond ((null? (cdr c_list)) #f)
((in-list? a_list c_list (cdr c_list)) #t)
(else (inner-cycles? (cdr c_list)))))
(inner-cycles? a_list))
(cs-cycles? list_cheeky_cycles)
(cs-cycles? list_cycles)
(not (cs-cycles? (list 1 2 3 4 5)))
; ToDo There's a much smarter way to do this in constant space and linear time.
"3.20 - done on paper"
; More environment diagrams
; Queues
(define (front-ptr queue) (car queue))
(define (rear-ptr queue) (cdr queue))
(define (set-front-ptr! queue item)
(set-car! queue item))
(define (set-rear-ptr! queue item)
(set-cdr! queue item))
(define (empty-queue? queue)
(null? (front-ptr queue)))
(define (make-queue) (cons '() '()))
(define (front-queue queue)
(if (empty-queue? queue)
(error "FRONT called with an
empty queue" queue)
(car (front-ptr queue))))
; make a new pair with the item to be inserted and the empty list '()
; if the queue was empty, set both pointers to this new pair
; else, set the cdr of the last item to this new pair and move the last item pointer
(define (insert-queue! queue item)
(let ((new-pair (cons item '())))
(cond ((empty-queue? queue)
(set-front-ptr! queue new-pair)
(set-rear-ptr! queue new-pair)
queue)
(else (set-cdr! (rear-ptr queue)
new-pair)
(set-rear-ptr! queue new-pair)
queue))))
; to take an item from the front of the queue, modify the front pointer and let the
; garbage collector do the rest
(define (delete-queue! queue)
(cond ((empty-queue? queue)
(error "DELETE! called with
an empty queue" queue))
(else (set-front-ptr!
queue
(cdr (front-ptr queue)))
queue)))
"Exercise 3.21"
(define (print-queue queue)
(display (front-ptr queue))
(display "\n"))
(define q1 (make-queue))
(insert-queue! q1 'a)
(insert-queue! q1 'b)
(print-queue q1)
(delete-queue! q1)
(delete-queue! q1)
(print-queue q1)
"Exercise 3.22"
(define (make-queue2)
(let ((front-ptr '())
(rear-ptr '()))
(define (insert item)
(let ((new-pair (cons item '())))
(cond ((null? front-ptr)
(set! front-ptr new-pair)
(set! rear-ptr new-pair))
(else (set-cdr! rear-ptr new-pair)
(set! rear-ptr new-pair))))
front-ptr)
(define (delete)
; remove the first item
(set! front-ptr (cdr front-ptr))
front-ptr)
(define (dispatch m)
(cond ((eq? m 'insert) insert)
((eq? m 'front) (car front-ptr))
((eq? m 'isempty) (null? front-ptr))
((eq? m 'delete) delete)
(else (error "Undefined operation: CONS" m))))
dispatch))
(define q2 (make-queue2))
;; we won't bother with convenience functions for these
((q2 'insert) 77)
((q2 'insert) 79)
(q2 'front)
(q2 'isempty)
((q2 'delete))
((q2 'delete))
(q2 'isempty)
"done"
"Exercise 3.23 Deques"
; Operations on deques are the constructor make-deque,
; the predicate empty-deque?, selectors front-deque and rear-deque,
; and mutators front-insert-deque!, rear-insert-deque!,
; front-delete-deque!, rear-delete-deque!. Show how to represent
; deques using pairs, and give implementations of the operations.
; All operations should be accomplished in Θ(1) steps.
; Deques are often implemented as doubly-linked lists so
; we'll keep the front and rear ptrs and each item will be
; '(previous-ptr item next-ptr)
;(define deqf (cons 'a '()))
;(define deqe (cons 'b deqf))
;(set-cdr! deqf deqe)
;(cons deqf deqe)
; We're doing a pair-based implementation
(define (make-deque)
(cons '() '()))
; If the front is empty then the deque is empty
(define (empty-deque? deque)
(null? (car deque)))
; Helpers
(define (get-front-deque deque)
(car deque))
(define (get-rear-deque deque)
(cdr deque))
; More helpers
(define (set-front-deque! deque triple)
(set-car! deque triple))
(define (set-rear-deque! deque triple)
(set-cdr! deque triple))
; Add an item to the rear of the deque
(define (rear-insert-deque! deque item)
; '(previous-rear . item . () . ())
(let ((new-triple (list (get-rear-deque deque) item '())))
(begin
(if (empty-deque? deque)
; then
(set-front-deque! deque new-triple)
; else
(set-car! (cddr (get-rear-deque deque)) new-triple))
(set-rear-ptr! deque new-triple)))
deque)
; Add an item to the front of the deque
(define (front-insert-deque! deque item)
; '(() . item . previous-front . ())
(let ((new-triple (list '() item (get-front-deque deque))))
(begin
(if (empty-deque? deque)
(set-rear-deque! deque new-triple)
(set-car! (get-front-deque deque) new-triple))
(set-front-ptr! deque new-triple)))
deque)
(define (rear-delete-deque! deque)
(cond ((empty-deque? deque)
(error "Cannot delete from an empty deque"))
((null? (caddr (get-front-deque deque)))
; only one element
(begin (set-front-deque! deque '())
(set-rear-deque! deque '())))
(else (begin (set-rear-deque! deque (car (get-rear-deque deque)))
(set-car! (cddr (get-rear-deque deque)) '())))))
; ToDo ...
(define (front-delete-deque! deque)
1)
; Testing
;; May be useful to have a simple way to print the deque contents
(define (display-deque deque)
(define (deque-to-list node)
(if (null? (caddr node))
(list (cadr node))
(cons (cadr node) (deque-to-list (caddr node)))))
(display (deque-to-list (car deque)))
(display "\n"))
(define my-deque (make-deque))
(empty-deque? my-deque)
(rear-insert-deque! my-deque 4)
(display-deque my-deque)
(rear-insert-deque! my-deque 99)
(front-insert-deque! my-deque 100)
;(get-rear-deque my-deque)
(display-deque my-deque)
(rear-delete-deque! my-deque)
(rear-delete-deque! my-deque)
(rear-delete-deque! my-deque)
my-deque
; 3.3.3. Representing Tables
;; Two-dimensional "local" tables
(define (assoc key records)
(cond ((null? records) false)
((equal? key (caar records))
(car records))
(else (assoc key (cdr records)))))
;; This is provided in 3.3.3.
(define (make-table)
(let ((local-table (list '*table*)))
(define (lookup key-1 key-2)
(let ((subtable
(assoc key-1 (cdr local-table))))
(if subtable
(let ((record
(assoc key-2
(cdr subtable))))
(if record (cdr record) false))
false)))
(define (insert! key-1 key-2 value)
(let ((subtable
(assoc key-1 (cdr local-table))))
(if subtable
(let ((record
(assoc key-2
(cdr subtable))))
(if record
(set-cdr! record value)
(set-cdr!
subtable
(cons (cons key-2 value)
(cdr subtable)))))
(set-cdr!
local-table
(cons (list key-1
(cons key-2 value))
(cdr local-table)))))
'ok)
(define (dispatch m)
(cond ((eq? m 'lookup-proc) lookup)
((eq? m 'insert-proc!) insert!)
(else (error "Unknown operation:
TABLE" m))))
dispatch))
(define operation-table (make-table))
(define get (operation-table 'lookup-proc))
(define put (operation-table 'insert-proc!))
(put 4 5 "a value")
(put 'a 'b "another value")
(get 4 5)
;; Exercise 3.24
;; Define a make-table procedure that takes same-key? as an argument.
(define (my-assoc key records is-equal?)
(cond ((null? records) false)
((is-equal? key (caar records))
(car records))
(else (my-assoc key (cdr records) is-equal?))))
(define (make-my-table same-key?)
(let ((local-table (list '*table*))
(asso (lambda (key records) (my-assoc key records same-key?))))
(define (lookup key-1 key-2)
(let ((subtable
(asso key-1 (cdr local-table))))
(if subtable
(let ((record
(asso key-2
(cdr subtable))))
(if record (cdr record) false))
false)))
(define (insert! key-1 key-2 value)
(let ((subtable
(asso key-1 (cdr local-table))))
(if subtable
(let ((record
(asso key-2
(cdr subtable))))
(if record
(set-cdr! record value)
(set-cdr!
subtable
(cons (cons key-2 value)
(cdr subtable)))))
(set-cdr!
local-table
(cons (list key-1
(cons key-2 value))
(cdr local-table)))))
'ok)
(define (dispatch m)
(cond ((eq? m 'lookup-proc) lookup)
((eq? m 'insert-proc!) insert!)
(else (error "Unknown operation:
TABLE" m))))
dispatch))
(define (plus-minus-one? a b)
(display "plus-minus-one?")
(display " ")
(display a)
(display " ")
(display b)
(display "\n")
(< (abs (- a b)) 1))
(plus-minus-one? 4.9 4)
(plus-minus-one? 4 4.9)
(not (plus-minus-one? 5 4))
(not (plus-minus-one? 4 5))
(define my-op-table (make-my-table plus-minus-one?))
(define my-get (my-op-table 'lookup-proc))
(define my-put (my-op-table 'insert-proc!))
"putting"
(my-put 4 5 "a value")
;(my-put 'a 'b "another value")
"getting"
(my-get 4.1 5.7)
;; Exercise 3.25
;; N-dimensional tables
;; Note, I don't really know why you'd want to do it this way, when you
;; could just treat the list of keys as a key and use a 1-d table...
;; I guess it's more space-efficient for lots of similar, very long, keys
;; We'll assume that *table* has been stripped from the beginning of table.
;; Get the value or sub-table associated with key
;(define (recursive-lookup key table)
; (cond ((null? table) false)
; ((equal? (caar table) key) (cdr (car table)))
; (else (recursive-lookup key (cdr table)))))
;(newline)
;"Recursive Lookup"
;(equal? (recursive-lookup 'a (list (cons 'a 44))) 44)
;(equal? (recursive-lookup 'a (list (cons 'b 55) (cons 'a 'z))) 'z)
;(equal? (recursive-lookup 1 (list (list 1 (cons 2 "end")))) (list (cons 2 "end")))
;(recursive-lookup 1 (list (list 1 (cons 2 "end"))))
;(not (recursive-lookup 'c (list (cons 'b 55) (cons 'a 'z))))
;(define (recursive-lookups keys table)
; (cond ((null? table) false)
; ((null? keys) table)
; ((equal? (caar table) (car keys)) (recursive-lookups (cdr keys) (cdr (car table))))))
;; A recursive lookup returning the first match or false
(define (lookup-2 keys table)
(cond ((null? table) false)
((null? keys) table)
((equal? (caadr table) (car keys)) (lookup-2 (cdr keys) (car (cdr table))))
(else false)))
(newline)
"Test lookup-2"
(equal? (lookup-2 '() (cons 'math 43)) (cons 'math 43))
(equal? (lookup-2 (list 2) (list 1 (cons 2 "oo"))) (cons 2 "oo"))
;; Like lookup-2 but to be used at the root level of the table
;; from the second element onwards
(define (lookup-1 keys table)
(cond ((null? table) false) ; table is completely empty
((null? keys) (error "Must have one or more key"))
((equal? (caar table) (car keys)) (lookup-2 (cdr keys) (car table)))
(else (lookup-1 keys (cdr table))))) ; didn't match
(newline)
"Test lookup-1"
(equal? false (lookup-1 (list "some key") '()))
(equal? (cons 2 "yes") (lookup-1 (list 1 2) (list (list 1 (cons 2 "yes")))))
(equal? (cons 3 "yes") (lookup-1 (list 3) (list (cons 'math 1) (cons 3 "yes"))))
(equal? (cons 6 "yes") (lookup-1 (list 4 5 6) (list (cons 1 "no")
(cons 2 "no")
(list 4 (list 5 (cons 6 "yes"))))))
(define (insert-2! keys value table)
(let ((found (lookup-2 (list (car keys)) table)))
(if found
(insert-2! (cdr keys) value found)
(let ((new-subtable (create-subtable keys value)))
(set-cdr! table ; insert it behind '*table*
(cons new-subtable
(cdr table)))))))
;; for level
(define (create-subtable keys value)
(if (null? (cdr keys))
(cons (car keys) value)
(list (car keys) (create-subtable (cdr keys) value))))
(newline)
"Test create-subtable"
(equal? (create-subtable (list 1) 'a) (cons 1 'a))
(equal? (create-subtable (list 1 2) 'a) (list 1 (cons 2 'a)))
; Insert value into table at the position given by keys
; (a position that must not exist already)
(define (insert-1! keys value table)
(let ((found (lookup-1 (list (car keys)) (cdr table))))
(if found
(insert-2! (cdr keys) value found) ; the first key is already there
(let ((new-subtable (create-subtable keys value)))
(set-cdr! table ; insert it behind '*table*
(cons new-subtable
(cdr table)))))))
(newline)
"Test insert-1!"
(define test-table-1 (list '*table*))
(insert-1! (list 1) "yes" test-table-1)
(equal? test-table-1 (list '*table* (cons 1 "yes")))
;(equal? (lookup-2 (list 1) (list (cons 1 "end"))))
(define (make-nd-table)
(let ((local-table (list '*table*)))
(define (lookup keys)
(let ((answer (lookup-1 keys (cdr local-table))))
(if answer
(cdr answer)
false)))
(define (insert! keys value)
(let ((answer (lookup-1 keys (cdr local-table))))
(if answer
(begin (set-cdr! answer value) 'overwritten)
(begin (insert-1! keys value local-table) 'ok))))