-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercises-2.1.html
1339 lines (1194 loc) · 50.1 KB
/
exercises-2.1.html
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
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>SICP Exercise Solutions for Section 2.1</title>
<!-- 2014-07-01 Tue 16:26 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="generator" content="Org-mode" />
<meta name="author" content="Paul L. Snyder" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center; }
.todo { font-family: monospace; color: red; }
.done { color: green; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.right { margin-left: auto; margin-right: 0px; text-align: right; }
.left { margin-left: 0px; margin-right: auto; text-align: left; }
.center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
pre.src-sh:before { content: 'sh'; }
pre.src-bash:before { content: 'sh'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-R:before { content: 'R'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-java:before { content: 'Java'; }
pre.src-sql:before { content: 'SQL'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.right { text-align: center; }
th.left { text-align: center; }
th.center { text-align: center; }
td.right { text-align: right; }
td.left { text-align: left; }
td.center { text-align: center; }
dt { font-weight: bold; }
.footpara:nth-child(2) { display: inline; }
.footpara { display: block; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
/*]]>*/-->
</style>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2013 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
<script type="text/javascript" src="http://orgmode.org/mathjax/MathJax.js"></script>
<script type="text/javascript">
<!--/*--><![CDATA[/*><!--*/
MathJax.Hub.Config({
// Only one of the two following lines, depending on user settings
// First allows browser-native MathML display, second forces HTML/CSS
// config: ["MMLorHTML.js"], jax: ["input/TeX"],
jax: ["input/TeX", "output/HTML-CSS"],
extensions: ["tex2jax.js","TeX/AMSmath.js","TeX/AMSsymbols.js",
"TeX/noUndefined.js"],
tex2jax: {
inlineMath: [ ["\\(","\\)"] ],
displayMath: [ ['$$','$$'], ["\\[","\\]"], ["\\begin{displaymath}","\\end{displaymath}"] ],
skipTags: ["script","noscript","style","textarea","pre","code"],
ignoreClass: "tex2jax_ignore",
processEscapes: false,
processEnvironments: true,
preview: "TeX"
},
showProcessingMessages: true,
displayAlign: "center",
displayIndent: "2em",
"HTML-CSS": {
scale: 100,
availableFonts: ["STIX","TeX"],
preferredFont: "TeX",
webFont: "TeX",
imageFont: "TeX",
showMathMenu: true,
},
MMLorHTML: {
prefer: {
MSIE: "MML",
Firefox: "MML",
Opera: "HTML",
other: "HTML"
}
}
});
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<h1 class="title">SICP Exercise Solutions for Section 2.1</h1>
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#sec-1">1. 2.1.1 Example: Arithmetic Operations for Rational Numbers</a>
<ul>
<li><a href="#sec-1-1">1.1. <span class="todo WRITEUP">WRITEUP</span> Exercise 2.1: Improving <code>make-rat</code></a>
<ul>
<li><a href="#sec-1-1-1">1.1.1. Problem</a></li>
<li><a href="#sec-1-1-2">1.1.2. Solution</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#sec-2">2. 2.1.2 Abstraction Barriers</a>
<ul>
<li><a href="#sec-2-1">2.1. <span class="todo WRITEUP">WRITEUP</span> Exercise 2.2: Representations for line segments</a>
<ul>
<li><a href="#sec-2-1-1">2.1.1. Problem</a></li>
<li><a href="#sec-2-1-2">2.1.2. Solution</a></li>
</ul>
</li>
<li><a href="#sec-2-2">2.2. Exercise 2.3: Rectangular representations</a>
<ul>
<li><a href="#sec-2-2-1">2.2.1. Problem</a></li>
<li><a href="#sec-2-2-2">2.2.2. Solution</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#sec-3">3. 2.1.3 What Is Meant by Data?</a>
<ul>
<li><a href="#sec-3-1">3.1. Exercise 2.4: Lambdas as data structures</a>
<ul>
<li><a href="#sec-3-1-1">3.1.1. Problem</a></li>
<li><a href="#sec-3-1-2">3.1.2. Solution</a></li>
</ul>
</li>
<li><a href="#sec-3-2">3.2. <span class="todo WRITEUP">WRITEUP</span> Exercise 2.5: Creative data representation</a>
<ul>
<li><a href="#sec-3-2-1">3.2.1. Problem</a></li>
<li><a href="#sec-3-2-2">3.2.2. Solution</a></li>
</ul>
</li>
<li><a href="#sec-3-3">3.3. <span class="todo WRITEUP">WRITEUP</span> Exercise 2.6: Church numerals</a>
<ul>
<li><a href="#sec-3-3-1">3.3.1. Problem</a></li>
<li><a href="#sec-3-3-2">3.3.2. Solution</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#sec-4">4. 2.1.4 Extended Exercise: Interval Arithmetic</a>
<ul>
<li><a href="#sec-4-1">4.1. <span class="todo WRITEUP">WRITEUP</span> Exercise 2.7: Selectors for interval arithmetic</a>
<ul>
<li><a href="#sec-4-1-1">4.1.1. Problem</a></li>
<li><a href="#sec-4-1-2">4.1.2. Solution</a></li>
</ul>
</li>
<li><a href="#sec-4-2">4.2. <span class="todo WRITEUP">WRITEUP</span> Exercise 2.8: Subtracting intervals</a>
<ul>
<li><a href="#sec-4-2-1">4.2.1. Problem</a></li>
<li><a href="#sec-4-2-2">4.2.2. Solution</a></li>
</ul>
</li>
<li><a href="#sec-4-3">4.3. Exercise 2.9: Interval widths</a>
<ul>
<li><a href="#sec-4-3-1">4.3.1. Problem</a></li>
<li><a href="#sec-4-3-2">4.3.2. Solution</a></li>
</ul>
</li>
<li><a href="#sec-4-4">4.4. <span class="todo WRITEUP">WRITEUP</span> Exercise 2.10: Intervals spanning zero and error signalling</a>
<ul>
<li><a href="#sec-4-4-1">4.4.1. Problem</a></li>
<li><a href="#sec-4-4-2">4.4.2. Solution</a></li>
</ul>
</li>
<li><a href="#sec-4-5">4.5. <span class="todo TODO">TODO</span> Exercise 2.11: Reducing multiplications for <code>mul-interval</code></a>
<ul>
<li><a href="#sec-4-5-1">4.5.1. Problem</a></li>
<li><a href="#sec-4-5-2">4.5.2. Solution</a></li>
</ul>
</li>
<li><a href="#sec-4-6">4.6. <span class="todo WRITEUP">WRITEUP</span> Exercise 2.12: Center-percentage representations for intervals</a>
<ul>
<li><a href="#sec-4-6-1">4.6.1. Problem</a></li>
<li><a href="#sec-4-6-2">4.6.2. Solution</a></li>
</ul>
</li>
<li><a href="#sec-4-7">4.7. <span class="todo TODO">TODO</span> Exercise 2.13: Approximating small tolerances</a>
<ul>
<li><a href="#sec-4-7-1">4.7.1. Problem</a></li>
<li><a href="#sec-4-7-2">4.7.2. Solution</a></li>
</ul>
</li>
<li><a href="#sec-4-8">4.8. <span class="todo TODO">TODO</span> Exercise 2.14: Problems with the interval representation</a>
<ul>
<li><a href="#sec-4-8-1">4.8.1. Problem</a></li>
<li><a href="#sec-4-8-2">4.8.2. Solution</a></li>
</ul>
</li>
<li><a href="#sec-4-9">4.9. <span class="todo TODO">TODO</span> Exercise 2.15: Examining the growth of uncertainty</a>
<ul>
<li><a href="#sec-4-9-1">4.9.1. Problem</a></li>
<li><a href="#sec-4-9-2">4.9.2. Solution</a></li>
</ul>
</li>
<li><a href="#sec-4-10">4.10. <span class="todo TODO">TODO</span> Exercise 2.16: On the non-equivalence of algebraic expressions</a>
<ul>
<li><a href="#sec-4-10-1">4.10.1. Problem</a></li>
<li><a href="#sec-4-10-2">4.10.2. Solution</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1"><span class="section-number-2">1</span> 2.1.1 Example: Arithmetic Operations for Rational Numbers</h2>
<div class="outline-text-2" id="text-1">
</div><div id="outline-container-sec-1-1" class="outline-3">
<h3 id="sec-1-1"><span class="section-number-3">1.1</span> <span class="todo WRITEUP">WRITEUP</span> Exercise 2.1: Improving <code>make-rat</code></h3>
<div class="outline-text-3" id="text-1-1">
</div><div id="outline-container-sec-1-1-1" class="outline-4">
<h4 id="sec-1-1-1"><span class="section-number-4">1.1.1</span> Problem</h4>
<div class="outline-text-4" id="text-1-1-1">
<p>
Define a better version of <code>make-rat</code> that handles both positive
and negative arguments. <code>make-rat</code> should normalize the sign so
that if the rational number is positive, both the numerator and
denominator are positive, and if the rational number is negative,
only the numerator is negative.
</p>
</div>
</div>
<div id="outline-container-sec-1-1-2" class="outline-4">
<h4 id="sec-1-1-2"><span class="section-number-4">1.1.2</span> Solution</h4>
<div class="outline-text-4" id="text-1-1-2">
<div class="org-src-container">
<pre class="src src-scheme">(<span style="color: #859900; font-weight: bold;">define</span> (<span style="color: #268bd2;">gcd</span> a b)
(<span style="color: #859900; font-weight: bold;">if</span> (= b 0)
a
(gcd b (remainder a b))))
(<span style="color: #859900; font-weight: bold;">define</span> (<span style="color: #268bd2;">make-rat</span> n d)
(<span style="color: #859900; font-weight: bold;">let</span> ((nf (<span style="color: #859900; font-weight: bold;">if</span> (< d 0) (- n) n))
(df (abs d))
(g (abs (gcd n d))))
(cons (/ nf g) (/ df g))))
</pre>
</div>
</div>
</div>
</div>
</div>
<div id="outline-container-sec-2" class="outline-2">
<h2 id="sec-2"><span class="section-number-2">2</span> 2.1.2 Abstraction Barriers</h2>
<div class="outline-text-2" id="text-2">
</div><div id="outline-container-sec-2-1" class="outline-3">
<h3 id="sec-2-1"><span class="section-number-3">2.1</span> <span class="todo WRITEUP">WRITEUP</span> Exercise 2.2: Representations for line segments</h3>
<div class="outline-text-3" id="text-2-1">
</div><div id="outline-container-sec-2-1-1" class="outline-4">
<h4 id="sec-2-1-1"><span class="section-number-4">2.1.1</span> Problem</h4>
<div class="outline-text-4" id="text-2-1-1">
<p>
Consider the problem of representing line segments
in a plane. Each segment is represented as a pair of points: a
starting point and an ending point. Define a constructor
<code>make-segment</code> and selectors <code>start-segment</code> and <code>end-segment</code>
that define the representation of segments in terms of points.
Furthermore, a point can be represented as a pair of numbers: the
\(x\) coordinate and the \(y\) coordinate. Accordingly, specify a
constructor <code>make-point</code> and selectors <code>x-point</code> and <code>y-point</code>
that define this representation. Finally, using your selectors
and constructors, define a procedure <code>midpoint-segment</code> that takes
a line segment as argument and returns its midpoint (the point
whose coordinates are the average of the coordinates of the
endpoints). To try your procedures, you'll need a way to print
points:
</p>
<div class="org-src-container">
<pre class="src src-scheme">(<span style="color: #859900; font-weight: bold;">define</span> (<span style="color: #268bd2;">print-point</span> p)
(newline)
(display <span style="color: #2aa198;">"("</span>)
(display (x-point p))
(display <span style="color: #2aa198;">","</span>)
(display (y-point p))
(display <span style="color: #2aa198;">")"</span>))
</pre>
</div>
</div>
</div>
<div id="outline-container-sec-2-1-2" class="outline-4">
<h4 id="sec-2-1-2"><span class="section-number-4">2.1.2</span> Solution</h4>
<div class="outline-text-4" id="text-2-1-2">
<div class="org-src-container">
<pre class="src src-scheme">(<span style="color: #859900; font-weight: bold;">define</span> (<span style="color: #268bd2;">make-point</span> x y)
(cons x y))
(<span style="color: #859900; font-weight: bold;">define</span> (<span style="color: #268bd2;">x-point</span> p)
(car p))
(<span style="color: #859900; font-weight: bold;">define</span> (<span style="color: #268bd2;">y-point</span> p)
(cdr p))
(<span style="color: #859900; font-weight: bold;">define</span> (<span style="color: #268bd2;">make-segment</span> a b)
(cons a b))
(<span style="color: #859900; font-weight: bold;">define</span> (<span style="color: #268bd2;">start-segment</span> s)
(car s))
(<span style="color: #859900; font-weight: bold;">define</span> (<span style="color: #268bd2;">end-segment</span> s)
(cdr s))
(<span style="color: #859900; font-weight: bold;">define</span> (<span style="color: #268bd2;">midpoint-segment</span> s)
(<span style="color: #859900; font-weight: bold;">let</span> ((x1 (x-point (start-segment s)))
(y1 (y-point (start-segment s)))
(x2 (x-point (end-segment s)))
(y2 (y-point (end-segment s))))
(make-point (/ (+ x1 x2) 2) (/ (+ y1 y2) 2))))
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-sec-2-2" class="outline-3">
<h3 id="sec-2-2"><span class="section-number-3">2.2</span> Exercise 2.3: Rectangular representations</h3>
<div class="outline-text-3" id="text-2-2">
</div><div id="outline-container-sec-2-2-1" class="outline-4">
<h4 id="sec-2-2-1"><span class="section-number-4">2.2.1</span> Problem</h4>
<div class="outline-text-4" id="text-2-2-1">
<p>
Implement a representation for rectangles in a plane. (Hint: You
may want to make use of Exercise 2-2.) In terms of your
constructors and selectors, create procedures that compute the
perimeter and the area of a given rectangle. Now implement a
different representation for rectangles. Can you design your
system with suitable abstraction barriers, so that the same
perimeter and area procedures will work using either
representation?
</p>
</div>
</div>
<div id="outline-container-sec-2-2-2" class="outline-4">
<h4 id="sec-2-2-2"><span class="section-number-4">2.2.2</span> Solution</h4>
<div class="outline-text-4" id="text-2-2-2">
<p>
One possibility is to represent a rectangle using any two points in the
plane. For tidiness, we can normalize the internal representation
of these much as we did for exercise 2.1.
</p>
<div class="org-src-container">
<pre class="src src-scheme">(<span style="color: #859900; font-weight: bold;">define</span> (<span style="color: #268bd2;">make-rectangle</span> point1 point2)
<span style="color: #586e75;">;; </span><span style="color: #586e75;">normalize to top-left and bottom-right points</span>
(<span style="color: #859900; font-weight: bold;">let</span> ((x1 (min (x-point point1) (x-point point2)))
(x2 (max (x-point point1) (x-point point2)))
(y1 (max (y-point point1) (y-point point2)))
(y2 (min (y-point point1) (y-point point2))))
(cons (make-point x1 y1) (make-point x2 y2))))
(<span style="color: #859900; font-weight: bold;">define</span> (<span style="color: #268bd2;">top-left-rectangle</span> r)
(car r))
(<span style="color: #859900; font-weight: bold;">define</span> (<span style="color: #268bd2;">bottom-right-rectangle</span> r)
(cdr r))
(<span style="color: #859900; font-weight: bold;">define</span> (<span style="color: #268bd2;">width-rectangle</span> r)
(<span style="color: #859900; font-weight: bold;">let</span> ((x1 (x-point (top-left-rectangle r)))
(x2 (x-point (bottom-right-rectangle r))))
(abs (- x2 x1))))
(<span style="color: #859900; font-weight: bold;">define</span> (<span style="color: #268bd2;">height-rectangle</span> r)
(<span style="color: #859900; font-weight: bold;">let</span> ((y1 (y-point (top-left-rectangle r)))
(y2 (y-point (bottom-right-rectangle r))))
(abs (- y1 y2))))
(<span style="color: #859900; font-weight: bold;">define</span> (<span style="color: #268bd2;">area-rectangle</span> r)
(* (width-rectangle r) (height-rectangle r)))
(<span style="color: #859900; font-weight: bold;">define</span> (<span style="color: #268bd2;">perimeter-rectangle</span> r)
(* 2 (+ (width-rectangle r) (height-rectangle r))))
</pre>
</div>
<p>
For another representation we could construct a representation from a
point and offsets from this point in terms of width and height.
</p>
<p>
If we were constructing a real representation (rather than just
working with an exercise in a book), we might like to do some
additional normalization for this representation as well. In this
case, though, we'll just go with a simple constructor.
</p>
<p>
Since we haven't been introduced to robust error-checking mechanisms,
yet (such as contracts in Racket), we'll just assume that the width
and height provided are positive, and providing negative values will
result in undefined behavior.
</p>
<div class="org-src-container">
<pre class="src src-scheme">(<span style="color: #859900; font-weight: bold;">define</span> (<span style="color: #268bd2;">make-rectangle</span> point width height)
(cons point (cons width height)))
(<span style="color: #859900; font-weight: bold;">define</span> (<span style="color: #268bd2;">bottom-right-rectangle</span> r)
(<span style="color: #859900; font-weight: bold;">let</span> ((x1 (x-point (top-left-rectangle r)))
(y1 (y-point (top-left-rectangle r))))
(make-point (+ x1 (car (cdr r))) (+ y1 (cdr (cdr r))))))
</pre>
</div>
<p>
This isn't the most satisfying reimplementation, not least because
we're missing a most important language feature: polymorphism: there's
no way for a procedure to detect which internal representation is
being used.
</p>
<p>
We could get around this by have this alternate constructor produce an
internal representation that's the same as the first version, but
that's not the description of the problem. For the moment, we'll just
note that none of the other procedures need to be changed for this to
work, but that based on the facilities we have available to us right
now we can't easily use rectangles that use differing internal
representations.
</p>
</div>
</div>
</div>
</div>
<div id="outline-container-sec-3" class="outline-2">
<h2 id="sec-3"><span class="section-number-2">3</span> 2.1.3 What Is Meant by Data?</h2>
<div class="outline-text-2" id="text-3">
</div><div id="outline-container-sec-3-1" class="outline-3">
<h3 id="sec-3-1"><span class="section-number-3">3.1</span> Exercise 2.4: Lambdas as data structures</h3>
<div class="outline-text-3" id="text-3-1">
</div><div id="outline-container-sec-3-1-1" class="outline-4">
<h4 id="sec-3-1-1"><span class="section-number-4">3.1.1</span> Problem</h4>
<div class="outline-text-4" id="text-3-1-1">
<p>
Here is an alternative procedural representation
of pairs. For this representation, verify that <code>(car (cons x y))</code>
yields <code>x</code> for any objects <code>x</code> and <code>y</code>.
</p>
<pre class="example">
(define (cons x y)
(lambda (m) (m x y)))
(define (car z)
(z (lambda (p q) p)))
</pre>
<p>
What is the corresponding definition of <code>cdr</code>? (Hint: To verify
that this works, make use of the substitution model of section
1.1.5.)
</p>
</div>
</div>
<div id="outline-container-sec-3-1-2" class="outline-4">
<h4 id="sec-3-1-2"><span class="section-number-4">3.1.2</span> Solution</h4>
<div class="outline-text-4" id="text-3-1-2">
<p>
First, a definition for <code>cdr</code> using this approach:
</p>
<pre class="example">
(define (cdr z)
(z (lambda (p q) q)))
</pre>
<p>
Now, we verify these alternate versions of both <code>car</code> and <code>cdr</code>.
First, to verify <code>car</code> we follow the suggestion in the exercise and
use the substitution method.
</p>
<pre class="example">
1> (car (cons x y))
2> (car (lambda (m) (m x y)))
3> ((lambda (m) (m x y)) (lambda (p q) p))
4> ((lambda (p q) p) x y)
5> ((lambda (x y) x))
6> x
</pre>
<p>
Verifying the alternative version of <code>cdr</code> follows exactly the same
pattern.
</p>
<pre class="example">
1> (cdr (cons x y))
2> (cdr (lambda (m) (m x y)))
3> ((lambda (m) (m x y)) (lambda (p q) q))
4> ((lambda (p q) q) x y)
5> ((lambda (x y) y))
6> y
</pre>
</div>
</div>
</div>
<div id="outline-container-sec-3-2" class="outline-3">
<h3 id="sec-3-2"><span class="section-number-3">3.2</span> <span class="todo WRITEUP">WRITEUP</span> Exercise 2.5: Creative data representation</h3>
<div class="outline-text-3" id="text-3-2">
</div><div id="outline-container-sec-3-2-1" class="outline-4">
<h4 id="sec-3-2-1"><span class="section-number-4">3.2.1</span> Problem</h4>
<div class="outline-text-4" id="text-3-2-1">
<p>
Show that we can represent pairs of non-negative integers using
only numbers and arithmetic operations if we represent the pair
\(a\) and \(b\) as the integer that is the product \(2^a 3^b\). Give
the corresponding definitions of the procedures <code>cons</code>, <code>car</code>,
and <code>cdr</code>.
</p>
</div>
</div>
<div id="outline-container-sec-3-2-2" class="outline-4">
<h4 id="sec-3-2-2"><span class="section-number-4">3.2.2</span> Solution</h4>
<div class="outline-text-4" id="text-3-2-2">
<p>
This one is fun. We can reuse the <code>expt</code> procedure for our encoding,
but need a specific answer to decode a pair: the number of times 2
occurs as a factor of the pair is the value of \(a\), and the number of
time 3 occurs as a factor is the value of \(b\). Rather than implement
it separately for our <code>icar</code> and <code>icdr</code> procedures, it's best to
capture this in another descriptive procedure: <code>factor-count</code>.
</p>
<div class="org-src-container">
<pre class="src src-scheme">(<span style="color: #859900; font-weight: bold;">define</span> (<span style="color: #268bd2;">icons</span> a b)
(* (expt 2 a) (expt 3 b)))
(<span style="color: #859900; font-weight: bold;">define</span> (<span style="color: #268bd2;">factor-count</span> i f)
(<span style="color: #859900; font-weight: bold;">define</span> (<span style="color: #268bd2;">iter</span> i c)
(<span style="color: #859900; font-weight: bold;">if</span> (= (remainder i f) 0)
(iter (/ i f) (+ c 1))
c))
(iter i 0))
(<span style="color: #859900; font-weight: bold;">define</span> (<span style="color: #268bd2;">icar</span> p)
(factor-count p 2))
(<span style="color: #859900; font-weight: bold;">define</span> (<span style="color: #268bd2;">icdr</span> p)
(factor-count p 3))
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-sec-3-3" class="outline-3">
<h3 id="sec-3-3"><span class="section-number-3">3.3</span> <span class="todo WRITEUP">WRITEUP</span> Exercise 2.6: Church numerals</h3>
<div class="outline-text-3" id="text-3-3">
</div><div id="outline-container-sec-3-3-1" class="outline-4">
<h4 id="sec-3-3-1"><span class="section-number-4">3.3.1</span> Problem</h4>
<div class="outline-text-4" id="text-3-3-1">
<p>
In case representing pairs as procedures wasn't mind-boggling
enough, consider that, in a language that can manipulate
procedures, we can get by without numbers (at least insofar as
non-negative integers are concerned) by implementing 0 and the
operation of adding 1 as
</p>
<pre class="example">
(define zero (lambda (f) (lambda (x) x)))
(define (add-1 n)
(lambda (f) (lambda (x) (f ((n f) x)))))
</pre>
<p>
This representation is known as "Church numerals", after its
inventor, Alonzo Church, the logician who invented the \(\lambda\)
calculus.
</p>
<p>
Define <code>one</code> and <code>two</code> directly (not in terms of <code>zero</code> and
<code>add-1</code>). (Hint: Use substitution to evaluate <code>(add-1 zero)</code>).
Give a direct definition of the addition procedure `+' (not in
terms of repeated application of `add-1').
</p>
</div>
</div>
<div id="outline-container-sec-3-3-2" class="outline-4">
<h4 id="sec-3-3-2"><span class="section-number-4">3.3.2</span> Solution</h4>
<div class="outline-text-4" id="text-3-3-2">
<p>
There's another sneaky thing going on here. Note that <code>add-1</code> is
defined as a <i>procedure</i>…this means that there's really an implicit
lambda here that the syntactic sugar obscures. I wish that the authors
hadn't done this, as it obscures the clarity of the substitution
process. To make things a bit clearly, I'll use a slightly different
(but effectively identical) desugared definition of <code>add-1</code>:
</p>
<pre class="example">
(define add-1 (lambda (n)
(lambda (f) (lambda (x) (f ((n f) x))))))
</pre>
<p>
Now, we can use this to shuffling all of these symbols around to try
to come up with a direct definition of <code>one</code>:
</p>
<pre class="example">
1> (add-1 zero)
2> (add-1 (lambda (f) (lambda (x) x)))
3> ((lambda (n)
(lambda (f)
(lambda (x)
(f ((n f) x))))
(lambda (f) (lambda (x) x)))) ;; this line holds 'zero'
</pre>
<p>
In step 3, note how <code>add-1</code>'s lambda is substituted into the execution
position. Thus, <code>zero</code> becomes the argument <code>n</code> and is substituted
into the body of <code>add-1</code>:
</p>
<pre class="example">
4> (lambda (f)
(lambda (x)
(f (( (lambda (f) (lambda (x) x)) ) f) x)))
</pre>
<p>
Whew! Keeping all of the parentheses straight is pretty painful. The
really important thing to note here is that <code>zero</code> lands not only in
execution position, but in a <i>nested</i> execution position: it's going
to be called itself, and then its resulting lambda will be called as
well.
</p>
<pre class="example">
5> (lambda (f)
(lambda (x)
(f ((lambda (x) x)) x)))
</pre>
<p>
After the first of these substitutions, the inner <code>f</code> disappears.
Look again at the definition of <code>zero</code> to see why this must be so.
</p>
<pre class="example">
5> (lambda (f)
(lambda (x)
(f x)))
</pre>
<p>
And here's the tasty surprise: we end up with a lambda that performs a
single function application of its argument <code>f</code> on the argument to its
inner lambda! Note that neither of these lambdas land in an execution
position, so they can't be substituted out. This gives us our new,
direct definition for <code>one</code>:
</p>
<pre class="example">
(define one (lambda (f) (lambda (x) (f x))))
</pre>
<p>
With this in hand, we can take it to the next step: a definition for
<code>two</code>. (You can probably guess what it is, if you've followed the
logic of the substitutions to this point.)
</p>
<p>
(define add-1 (lambda (n)
(lambda (f) (lambda (x) (f ((n f) x))))))
</p>
<pre class="example">
1> (add-1 one)
2> (add-1 (lambda (f) (lambda (x) (f x))))
3> ((lambda (n)
(lambda (f)
(lambda (x)
(f ((n f) x)))))
(lambda (f) (lambda (x) (f x))))
</pre>
<p>
Having fought through <code>one</code>, teasing this out isn't too bad. The
hardest part is keeping track of the scoping (that is, which nested
<code>f</code> is associated with which lambda.
</p>
<pre class="example">
4> (lambda (f)
(lambda (x)
(f (( (lambda (f) (lambda (x) (f x))) f) x))))
5> (lambda (f)
(lambda (x)
(f ( (lambda (x) (f x)) x))))
</pre>
<p>
Look carefully at the inner <code>lambda (f)</code> and the outer <code>lambda (f)</code>;
the outer <code>f</code> is substituted into the inner for step 5. This is a
likely source of confusion.
</p>
<pre class="example">
6> (lambda (f)
(lambda (x)
(f (f x))))
</pre>
<p>
The same thing happens to inner and outer <code>x</code> in step 6. Yikes! But
the result is reassuringly concise. (There are no prizes for guessing
what the definition for <code>three</code> is going to be!)
</p>
<p>
So: we can see that a Church numeral is represented by a number of
repeated applications of the same function to some abstract <code>x</code> at the
bottom of the chain.
</p>
<p>
The jump from this to addition is actually extremely straightforward.
Let's say we have two Church numerals is this representation, <code>p</code> and
<code>q</code>: each is represented by a stack of nested calls to <code>f</code>. The goal
is to merge those two stacks together.
</p>
<p>
Remember that each of those stacks is applied to some base
case…which could simply be the result of the other stack! Thus,
crafting a definition is actually quite straightforward (again, as
long as we keep our nested function definitions straight). <code>q</code> needs
to be applied to <code>x</code>, and then <code>p</code> needs to be applied to be result of
that operation. Each of these stacks must also be passed the <code>f</code> that
is being used, as well. This gives us our result:
</p>
<pre class="example">
(define (add p q)
(lambda (f)
(lambda (x)
((p f) ((q f) x)))))
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-sec-4" class="outline-2">
<h2 id="sec-4"><span class="section-number-2">4</span> 2.1.4 Extended Exercise: Interval Arithmetic</h2>
<div class="outline-text-2" id="text-4">
</div><div id="outline-container-sec-4-1" class="outline-3">
<h3 id="sec-4-1"><span class="section-number-3">4.1</span> <span class="todo WRITEUP">WRITEUP</span> Exercise 2.7: Selectors for interval arithmetic</h3>
<div class="outline-text-3" id="text-4-1">
</div><div id="outline-container-sec-4-1-1" class="outline-4">
<h4 id="sec-4-1-1"><span class="section-number-4">4.1.1</span> Problem</h4>
<div class="outline-text-4" id="text-4-1-1">
<p>
Alyssa's program is incomplete because she has not
specified the implementation of the interval abstraction. Here is
a definition of the interval constructor:
</p>
<div class="org-src-container">
<pre class="src src-scheme">(<span style="color: #859900; font-weight: bold;">define</span> (<span style="color: #268bd2;">make-interval</span> a b)
(cons a b))
</pre>
</div>
<p>
Define selectors <code>upper-bound</code> and <code>lower-bound</code> to complete the
implementation.
</p>
</div>
</div>
<div id="outline-container-sec-4-1-2" class="outline-4">
<h4 id="sec-4-1-2"><span class="section-number-4">4.1.2</span> Solution</h4>
<div class="outline-text-4" id="text-4-1-2">
<p>
First, we reproduce the definitions of Alyssa's procedures from the text:
</p>
<div class="org-src-container">
<pre class="src src-scheme">(<span style="color: #859900; font-weight: bold;">define</span> (<span style="color: #268bd2;">add-interval</span> x y)
(make-interval (+ (lower-bound x) (lower-bound y))
(+ (upper-bound x) (upper-bound y))))
(<span style="color: #859900; font-weight: bold;">define</span> (<span style="color: #268bd2;">mul-interval</span> x y)
(<span style="color: #859900; font-weight: bold;">let</span> ((p1 (* (lower-bound x) (lower-bound y)))
(p2 (* (lower-bound x) (upper-bound y)))
(p3 (* (upper-bound x) (lower-bound y)))
(p4 (* (upper-bound x) (upper-bound y))))
(make-interval (min p1 p2 p3 p4)
(max p1 p2 p3 p4))))
(<span style="color: #859900; font-weight: bold;">define</span> (<span style="color: #268bd2;">div-interval</span> x y)
(mul-interval x
(make-interval (/ 1.0 (upper-bound y))
(/ 1.0 (lower-bound y)))))
</pre>
</div>
<p>
The definitions of <code>upper-bound</code> and <code>lower-bound</code> are extremely
straightforward, as long as we remember that the pair is not
ordered. It might be desirable to have the <code>make-interval</code> constructor
function check to make sure the <code>car</code> is always the lower bound, but
here we're working with the definition as specified in the problem.
</p>
<div class="org-src-container">
<pre class="src src-scheme">(<span style="color: #859900; font-weight: bold;">define</span> (<span style="color: #268bd2;">upper-bound</span> i)
(<span style="color: #859900; font-weight: bold;">if</span> (> (car i) (cdr i))
(car i)
(cdr i)))
(<span style="color: #859900; font-weight: bold;">define</span> (<span style="color: #268bd2;">lower-bound</span> i)
(<span style="color: #859900; font-weight: bold;">if</span> (< (car i) (cdr i))
(car i)
(cdr i)))
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-sec-4-2" class="outline-3">
<h3 id="sec-4-2"><span class="section-number-3">4.2</span> <span class="todo WRITEUP">WRITEUP</span> Exercise 2.8: Subtracting intervals</h3>
<div class="outline-text-3" id="text-4-2">
</div><div id="outline-container-sec-4-2-1" class="outline-4">
<h4 id="sec-4-2-1"><span class="section-number-4">4.2.1</span> Problem</h4>
<div class="outline-text-4" id="text-4-2-1">
<p>
Using reasoning analogous to Alyssa's, describe how the
difference of two intervals may be computed. Define a
corresponding subtraction procedure, called <code>sub-interval</code>.
</p>
</div>
</div>
<div id="outline-container-sec-4-2-2" class="outline-4">
<h4 id="sec-4-2-2"><span class="section-number-4">4.2.2</span> Solution</h4>
<div class="outline-text-4" id="text-4-2-2">
<p>
This one is a little bit tricky: the "analogous reasoning" that we
need to do is to figure out the smallest and largest possible results
of the operation. For the smallest, the lowest value we can achieve
is when we subtract the highest possible value of the second interval
for the lowest possible value of the first. Similarly, the largest
results could occur if the first interval is at its highest value and
the second interval is at its lowest.
</p>
<div class="org-src-container">
<pre class="src src-scheme">(<span style="color: #859900; font-weight: bold;">define</span> (<span style="color: #268bd2;">sub-interval</span> x y)
(make-interval (- (lower-bound x) (upper-bound y))
(- (upper-bound x) (lower-bound y))))
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-sec-4-3" class="outline-3">
<h3 id="sec-4-3"><span class="section-number-3">4.3</span> Exercise 2.9: Interval widths</h3>
<div class="outline-text-3" id="text-4-3">
</div><div id="outline-container-sec-4-3-1" class="outline-4">
<h4 id="sec-4-3-1"><span class="section-number-4">4.3.1</span> Problem</h4>
<div class="outline-text-4" id="text-4-3-1">
<p>
The "width" of an interval is half of the difference between its
upper and lower bounds. The width is a measure of the
uncertainty of the number specified by the interval. For some
arithmetic operations the width of the result of combining two
intervals is a function only of the widths of the argument
intervals, whereas for others the width of the combination is not
a function of the widths of the argument intervals. Show that
the width of the sum (or difference) of two intervals is a
function only of the widths of the intervals being added (or
subtracted). Give examples to show that this is not true for
multiplication or division.