-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbook-Z-H-12.html
1260 lines (1049 loc) · 69.5 KB
/
book-Z-H-12.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
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<!-- Generated from TeX source by tex2page, v 4n,
(c) Dorai Sitaram, http://www.cs.rice.edu/~dorai/tex2page -->
<head>
<title>
Structure and Interpretation
of Computer Programs
</title>
<link rel="stylesheet" type="text/css" href="book-Z-C.css" title=default>
<meta name=robots content="noindex,follow">
</head>
<body>
<p><div class=navigation>[Go to <a href="book.html">first</a>,
<a href="book-Z-H-11.html">previous</a>,
<a href="book-Z-H-13.html">next</a> page; <a href="book-Z-H-4.html#%_toc_start">contents</a>; <a href="book-Z-H-38.html#%_index_start">index</a>]</div><p>
<a name="%_sec_1.3"></a>
<h2><a href="book-Z-H-4.html#%_toc_%_sec_1.3">1.3 Formulating Abstractions with Higher-Order Procedures</a></h2><p>
<p>
We have seen that procedures are, in effect, abstractions that describe
compound operations on numbers independent of the particular numbers.
For example, when we<p>
<p><tt><a name="%_idx_962"></a>(define (cube x) (* x x x))<br>
</tt><p>
we are not talking about the cube of a particular number, but rather
about a method for obtaining the cube of any number. Of course we
could get along without ever defining this procedure, by
always writing expressions such as<p>
<p><tt>(* 3 3 3)<br>
(* x x x)<br>
(* y y y) <br>
</tt><p>
and never mentioning <tt>cube</tt> explicitly. This would place us at a
serious disadvantage, forcing us to work always at the level of the
particular operations that happen to be primitives in the language
(multiplication, in this case) rather than in terms of higher-level
operations. Our programs would be able to compute cubes, but our
language would lack the ability to express the concept of cubing. One
of the things we should demand from a powerful programming language is
the ability to build abstractions by assigning names to common
patterns and then to work in terms of the abstractions directly.
Procedures provide this ability. This is why all but the most
primitive programming languages include mechanisms for defining
procedures.<p>
Yet even in numerical processing we will be severely limited in our
ability to create abstractions if we are restricted to procedures
whose parameters must be numbers. Often the same programming pattern
will be used with a number of different procedures. To express such
patterns as concepts, we will need to construct procedures that can
accept procedures as arguments or return procedures as values.
Procedures that manipulate procedures are called <a name="%_idx_964"></a><em>higher-order
procedures</em>. This section shows how higher-order procedures can serve
as powerful abstraction mechanisms, vastly increasing the expressive
power of our language.<p>
<a name="%_sec_1.3.1"></a>
<h3><a href="book-Z-H-4.html#%_toc_%_sec_1.3.1">1.3.1 Procedures as Arguments</a></h3><p>
<a name="%_idx_966"></a><a name="%_idx_968"></a>
Consider the following three procedures. The first computes the sum
of the integers from <tt>a</tt> through <tt>b</tt>:<p>
<p><tt><a name="%_idx_970"></a>(define (sum-integers a b)<br>
(if (> a b)<br>
0<br>
(+ a (sum-integers (+ a 1) b))))<br>
</tt><p>
The second computes the sum of the cubes of the integers in the given range:<p>
<p><tt><a name="%_idx_972"></a>(define (sum-cubes a b)<br>
(if (> a b)<br>
0<br>
(+ (cube a) (sum-cubes (+ a 1) b))))<br>
</tt><p>
The third computes the sum of a sequence of terms in the
series
<p><div align=left><img src="ch1-Z-G-26.gif"></div><p>
which converges to <img src="book-Z-G-D-9.gif">/8 (very slowly):<a name="call_footnote_Temp_77" href="#footnote_Temp_77"><sup><small>49</small></sup></a><p>
<p><tt><a name="%_idx_978"></a>(define (pi-sum a b)<br>
(if (> a b)<br>
0<br>
(+ (/ 1.0 (* a (+ a 2))) (pi-sum (+ a 4) b))))<br>
</tt><p><p>
These three procedures clearly share a common underlying pattern.
They are for the most part identical, differing only in the name of
the procedure, the function of <tt>a</tt> used to compute the term to be added,
and the function that provides the next value of <tt>a</tt>. We could generate
each of the procedures by filling in slots in the same template:<p>
<p><tt>(define (<<em>name</em>> a b)<br>
(if (> a b)<br>
0<br>
(+ (<<em>term</em>> a)<br>
(<<em>name</em>> (<<em>next</em>> a) b))))<br>
</tt><p><p>
<a name="%_idx_980"></a>The presence of such a common pattern is strong evidence that there is
a useful abstraction waiting to be brought to the surface. Indeed,
mathematicians long ago identified the abstraction of
<a name="%_idx_982"></a><a name="%_idx_984"></a><em>summation of a series</em> and invented ``sigma
<a name="%_idx_986"></a><a name="%_idx_988"></a>notation,'' for example<p>
<p><div align=left><img src="ch1-Z-G-27.gif"></div><p><p>
to express this concept. The power of sigma notation is that it
allows mathematicians to deal with the concept of summation
itself rather than only with particular sums -- for example, to
formulate general results about sums that are independent of the
particular series being summed.<p>
Similarly, as program designers, we would like our language to
be powerful enough so that we can write a procedure that expresses the
concept of summation itself rather than only procedures
that compute particular sums. We can do so readily in our
procedural language by taking the common template shown above and
transforming the ``slots'' into formal parameters:<p>
<p><tt><a name="%_idx_990"></a>(define (sum term a next b)<br>
(if (> a b)<br>
0<br>
(+ (term a)<br>
(sum term (next a) next b))))<br>
</tt><p>
Notice that <tt>sum</tt> takes as its arguments the lower and upper bounds
<tt>a</tt> and <tt>b</tt> together with the procedures <tt>term</tt> and <tt>next</tt>.
We can use <tt>sum</tt> just as we would any procedure. For example, we can
use it (along with a procedure <tt>inc</tt> that increments its argument by 1)
to define <tt>sum-cubes</tt>:<p>
<p><tt><a name="%_idx_992"></a>(define (inc n) (+ n 1))<br>
<br>
<a name="%_idx_994"></a>(define (sum-cubes a b)<br>
(sum cube a inc b))<br>
</tt><p>
Using this, we can compute the sum of the cubes of the integers from 1
to 10:<p>
<p><tt>(sum-cubes 1 10)<br>
<i>3025</i><br>
</tt><p>
With the aid of an identity procedure to compute the term, we can define
<tt>sum-integers</tt> in terms of <tt>sum</tt>:<p>
<p><tt><a name="%_idx_996"></a>(define (identity x) x)<br>
<br>
<a name="%_idx_998"></a>(define (sum-integers a b)<br>
(sum identity a inc b))<br>
</tt><p>
Then we can add up the integers from 1 to 10:<p>
<p><tt>(sum-integers 1 10)<br>
<i>55</i><br>
</tt><p>
We can also define <tt>pi-sum</tt> in the same way:<a name="call_footnote_Temp_78" href="#footnote_Temp_78"><sup><small>50</small></sup></a><p>
<p><tt><a name="%_idx_1000"></a>(define (pi-sum a b)<br>
(define (pi-term x)<br>
(/ 1.0 (* x (+ x 2))))<br>
(define (pi-next x)<br>
(+ x 4))<br>
(sum pi-term a pi-next b))<br>
</tt><p>
Using these procedures, we can compute an approximation to
<img src="book-Z-G-D-9.gif">:<p>
<p><tt>(* 8 (pi-sum 1 1000))<br>
<i>3.139592655589783</i><br>
</tt><p><p>
Once we have <tt>sum</tt>, we can use it as a building block in
formulating further concepts. For instance, the <a name="%_idx_1002"></a>definite integral of a
function <em>f</em> between the limits <em>a</em> and <em>b</em> can be approximated
numerically using the formula
<p><p><div align=left><img src="ch1-Z-G-28.gif"></div><p>
for small values of <em>d</em><em>x</em>. We can express this directly as a
procedure:<p>
<p><tt><a name="%_idx_1004"></a>(define (integral f a b dx)<br>
(define (add-dx x) (+ x dx))<br>
(* (sum f (+ a (/ dx 2.0)) add-dx b)<br>
dx))<br>
<br>
(integral cube 0 1 0.01)<br>
<i>.24998750000000042</i><br>
<br>
(integral cube 0 1 0.001)<br>
<i>.249999875000001</i><br>
</tt><p>
(The exact value of the integral of <tt>cube</tt> between 0 and 1 is 1/4.)<p>
<p><a name="%_thm_1.29"></a>
<b>Exercise 1.29.</b> <a name="%_idx_1006"></a>Simpson's Rule is a more accurate method of numerical integration than
the method illustrated above. Using Simpson's Rule, the integral of a
function <em>f</em> between <em>a</em> and <em>b</em> is approximated as
<p><div align=left><img src="ch1-Z-G-29.gif"></div><p><p>
where <em>h</em> = (<em>b</em> - <em>a</em>)/<em>n</em>, for some even integer <em>n</em>, and <em>y</em><sub><em>k</em></sub> = <em>f</em>(<em>a</em> + <em>k</em><em>h</em>).
(Increasing <em>n</em> increases the accuracy of the approximation.) Define
a procedure that takes as arguments <em>f</em>, <em>a</em>, <em>b</em>, and <em>n</em> and returns
the value of the integral, computed using Simpson's Rule.
Use your procedure to integrate <tt>cube</tt> between 0 and 1
(with <em>n</em> = 100 and <em>n</em> = 1000), and compare the results to those of the
<tt>integral</tt> procedure shown above.
<p><p>
<p><a name="%_thm_1.30"></a>
<b>Exercise 1.30.</b> <a name="%_idx_1008"></a>The <tt>sum</tt> procedure above generates a linear recursion. The
procedure can be rewritten so that the sum is performed iteratively.
Show how to do this by filling in the missing expressions in the
following definition:<p>
<p><tt>(define (sum term a next b)<br>
(define (iter a result)<br>
(if <<em>??</em>><br>
<<em>??</em>><br>
(iter <<em>??</em>> <<em>??</em>>)))<br>
(iter <<em>??</em>> <<em>??</em>>))<br>
</tt><p>
<p><p>
<p><a name="%_thm_1.31"></a>
<b>Exercise 1.31.</b>
<a name="%_idx_1010"></a><br>a. The <tt>sum</tt> procedure is only the simplest of a vast number of
similar abstractions that can be captured as higher-order procedures.<a name="call_footnote_Temp_82" href="#footnote_Temp_82"><sup><small>51</small></sup></a> Write an analogous procedure
called <tt>product</tt> that returns the product of the values of a
function at points over a given range.
Show how to define <a name="%_idx_1012"></a><tt>factorial</tt> in terms of
<tt>product</tt>. Also use <tt>product</tt> to compute approximations to
<a name="%_idx_1014"></a><img src="book-Z-G-D-9.gif"> using the formula<a name="call_footnote_Temp_83" href="#footnote_Temp_83"><sup><small>52</small></sup></a>
<p><div align=left><img src="ch1-Z-G-30.gif"></div><p><p>
b. If your <tt>product</tt>
procedure generates a recursive process, write one that generates
an iterative process.
If it generates an iterative process, write one that generates
a recursive process.
<p><p>
<p><a name="%_thm_1.32"></a>
<b>Exercise 1.32.</b> <a name="%_idx_1018"></a><a name="%_idx_1020"></a><a name="%_idx_1022"></a>a. Show that <tt>sum</tt> and <tt>product</tt>
(exercise <a href="#%_thm_1.31">1.31</a>) are both special cases of a still more
general notion called <tt>accumulate</tt> that combines a collection of
terms, using some general accumulation function:<p>
<p><tt>(accumulate combiner null-value term a next b)<br>
</tt><p>
<tt>Accumulate</tt> takes as arguments the same term and range
specifications as <tt>sum</tt> and <tt>product</tt>, together with a <tt>combiner</tt>
procedure (of two arguments) that specifies how the current
term is to be combined with the accumulation of the preceding terms
and a <tt>null-value</tt> that specifies what base value to use
when the terms run out. Write <tt>accumulate</tt>
and show how <tt>sum</tt> and <tt>product</tt> can both
be defined as simple calls to <tt>accumulate</tt>.<p>
<p><p>b. If your <tt>accumulate</tt>
procedure generates a recursive process, write one that generates
an iterative process.
If it generates an iterative process, write one that generates
a recursive process.
<p><p>
<p><a name="%_thm_1.33"></a>
<b>Exercise 1.33.</b> <a name="%_idx_1024"></a>You can obtain an even more general version of <tt>accumulate</tt>
(exercise <a href="#%_thm_1.32">1.32</a>) by introducing the notion of a <a name="%_idx_1026"></a><em>filter</em> on the terms to be combined. That is, combine only those
terms derived from values in the range that satisfy a specified
condition. The resulting <tt>filtered-accumulate</tt> abstraction takes
the same arguments as accumulate, together with an additional
predicate of one argument that specifies the filter. Write <tt>filtered-accumulate</tt> as a procedure. Show how to express the
following using <tt>filtered-accumulate</tt>:<p>
<p><p>a. the sum of the squares of the prime numbers in the interval <em>a</em> to
<em>b</em> (assuming that you have a <tt>prime?</tt> predicate already written)<p>
<p><p>b. the product of all the positive integers less than <em>n</em>
<a name="%_idx_1028"></a>that are relatively prime to <em>n</em> (i.e., all positive integers
<em>i</em> < <em>n</em> such that <em>G</em><em>C</em><em>D</em>(<em>i</em>,<em>n</em>) = 1).
<p><p>
<a name="%_sec_1.3.2"></a>
<h3><a href="book-Z-H-4.html#%_toc_%_sec_1.3.2">1.3.2 Constructing Procedures Using <tt>Lambda</tt></a></h3><p>
<p>
In using <tt>sum</tt> as in section <a href="#%_sec_1.3.1">1.3.1</a>,
it seems terribly awkward to have to define trivial procedures such as
<tt>pi-term</tt> and <tt>pi-next</tt> just so we can use them as arguments to
our higher-order procedure. Rather than define <tt>pi-next</tt> and <tt>pi-term</tt>, it would be more convenient
to have a way to directly specify ``the procedure that returns its
input incremented by 4'' and ``the procedure that returns the
reciprocal of its input times its input plus 2.'' We can do this by
introducing the special form <tt>lambda</tt>, which creates procedures.
Using <tt>lambda</tt> we can describe what we want as<p>
<p><tt>(lambda (x) (+ x 4))<br>
</tt><p>
and <p>
<p><tt>(lambda (x) (/ 1.0 (* x (+ x 2))))<br>
</tt><p>
Then our <tt>pi-sum</tt> procedure can be expressed without defining any
auxiliary procedures as<p>
<p><tt><a name="%_idx_1030"></a>(define (pi-sum a b)<br>
(sum (lambda (x) (/ 1.0 (* x (+ x 2))))<br>
a<br>
(lambda (x) (+ x 4))<br>
b))<br>
</tt><p><p>
Again using <tt>lambda</tt>, we can write the <tt>integral</tt> procedure
without having to define the auxiliary procedure <tt>add-dx</tt>:<p>
<p><tt><a name="%_idx_1032"></a>(define (integral f a b dx)<br>
(* (sum f<br>
(+ a (/ dx 2.0))<br>
(lambda (x) (+ x dx))<br>
b)<br>
dx))<br>
</tt><p><p>
<a name="%_idx_1034"></a><a name="%_idx_1036"></a><a name="%_idx_1038"></a><a name="%_idx_1040"></a><a name="%_idx_1042"></a>In general, <tt>lambda</tt> is used to create procedures in the same way as
<tt>define</tt>, except that <a name="%_idx_1044"></a>no name is specified for the procedure:<p>
<p><tt>(lambda (<<em>formal-parameters</em>>) <<em>body</em>>)<br>
</tt><p>
The resulting procedure is just as much a procedure as one that is
created using <tt>define</tt>. The only difference is that it has not
been associated with any name in the environment. In fact,<p>
<a name="%_idx_1046"></a><p><tt>(define (plus4 x) (+ x 4))<br>
</tt><p>
is equivalent to<p>
<p><tt>(define plus4 (lambda (x) (+ x 4)))<br>
</tt><p>
We can read a <tt>lambda</tt> expression as follows:
<p><tt>(lambda (x) (+ x 4))<br>
<img src="book-Z-G-D-16.gif"> <img src="book-Z-G-D-16.gif"> <img src="book-Z-G-D-16.gif"> <img src="book-Z-G-D-16.gif"> <img src="book-Z-G-D-16.gif"><br>
the procedure of an argument <tt>x</tt> that adds <tt>x</tt> and 4<br>
</tt><p>
<a name="%_idx_1048"></a><a name="%_idx_1050"></a><a name="%_idx_1052"></a>Like any expression that has a procedure as its value, a
<tt>lambda</tt> expression can be used as the operator in a combination such as<p>
<p><tt>((lambda (x y z) (+ x y (square z))) 1 2 3)<br>
<i>12</i><br>
</tt><p>
or, more generally, in any context where we would normally use a
procedure name.<a name="call_footnote_Temp_86" href="#footnote_Temp_86"><sup><small>53</small></sup></a><p>
<a name="%_sec_IGNORE"></a>
<h4><a href="book-Z-H-4.html#%_toc_%_sec_IGNORE">Using <tt>let</tt> to create local variables</a></h4><p>
<a name="%_idx_1058"></a><a name="%_idx_1060"></a>
Another use of <tt>lambda</tt> is in creating local variables.
We often need local variables in our procedures other than those that have
been bound as formal parameters. For example, suppose we wish to
compute the function<p>
<p><div align=left><img src="ch1-Z-G-31.gif"></div><p><p>
which we could also express as
<p><div align=left><img src="ch1-Z-G-32.gif"></div><p>
In writing a procedure to compute <em>f</em>, we would like to include as
local variables not only <em>x</em> and <em>y</em> but also the names of
intermediate quantities like <em>a</em> and <em>b</em>. One way to
accomplish this is to
use an auxiliary procedure to bind the local variables:<p>
<p><tt>(define (f x y)<br>
(define (f-helper a b)<br>
(+ (* x (square a))<br>
(* y b)<br>
(* a b)))<br>
(f-helper (+ 1 (* x y)) <br>
(- 1 y)))<br>
</tt><p><p>
Of course, we could use a <tt>lambda</tt> expression to specify an
anonymous procedure for binding our local variables. The body of
<tt>f</tt> then becomes a single call to that procedure:<p>
<p><tt>(define (f x y)<br>
((lambda (a b)<br>
(+ (* x (square a))<br>
(* y b)<br>
(* a b)))<br>
(+ 1 (* x y))<br>
(- 1 y)))<br>
</tt><p>
This construct is so useful that there is a special form called
<tt>let</tt> to make its use more convenient. Using <tt>let</tt>, the <tt>f</tt>
procedure could be written as<p>
<p><tt>(define (f x y)<br>
(let ((a (+ 1 (* x y)))<br>
(b (- 1 y)))<br>
(+ (* x (square a))<br>
(* y b)<br>
(* a b))))<br>
</tt><p>
<a name="%_idx_1062"></a><a name="%_idx_1064"></a>The general form of a <tt>let</tt> expression is<p>
<p><tt>(let ((<<em>var<sub>1</sub></em>> <<em>exp<sub>1</sub></em>>)<br>
(<<em>var<sub>2</sub></em>> <<em>exp<sub>2</sub></em>>)<br>
<img src="book-Z-G-D-18.gif"><br>
(<<em>var<sub><em>n</em></sub></em>> <<em>exp<sub><em>n</em></sub></em>>))<br>
<<em>body</em>>)<br>
</tt><p>
which can be thought of as saying<p>
<table><tr><td valign=top >let </td><td valign=top ><<em>var<sub>1</sub></em>> have the value <<em>exp<sub>1</sub></em>> and</td></tr>
<tr><td valign=top ></td><td valign=top ><<em>var<sub>2</sub></em>> have the value <<em>exp<sub>2</sub></em>> and</td></tr>
<tr><td valign=top ></td><td valign=top ><img src="book-Z-G-D-18.gif"></td></tr>
<tr><td valign=top ></td><td valign=top ><<em>var<sub><em>n</em></sub></em>> have the value <<em>exp<sub><em>n</em></sub></em>></td></tr>
<tr><td valign=top >in </td><td valign=top ><<em>body</em>>
</td></tr></table><p>
The first part of the <tt>let</tt> expression is a list of
name-expression pairs. When the <tt>let</tt> is evaluated, each name is
associated with the value of the corresponding expression. The body
of the <tt>let</tt> is evaluated with
these names bound as local variables. The way this happens is that the <tt>let</tt> expression is interpreted as an alternate syntax for<p>
<p><tt>((lambda (<<em>var<sub>1</sub></em>> <tt>...</tt> <<em>var<sub><em>n</em></sub></em>>)<br>
<<em>body</em>>)<br>
<<em>exp<sub>1</sub></em>><br>
<img src="book-Z-G-D-18.gif"><br>
<<em>exp<sub><em>n</em></sub></em>>)<br>
</tt><p>
No new mechanism is required in the interpreter in order to
provide local variables. A <a name="%_idx_1066"></a><a name="%_idx_1068"></a><tt>let</tt> expression is simply syntactic sugar for
the underlying <tt>lambda</tt> application.<p>
<a name="%_idx_1070"></a><a name="%_idx_1072"></a>We can see from this equivalence that
the scope of a variable specified by a <tt>let</tt> expression is the body of
the <tt>let</tt>.
This implies that:<p>
<p><ul>
<li><tt>Let</tt> allows one to
bind variables as locally as possible to where they
are to be used. For example, if the value of <tt>x</tt> is 5,
the value of the expression<p>
<p><tt>(+ (let ((x 3))<br>
(+ x (* x 10)))<br>
x)<br>
</tt><p><p>
is 38. Here, the <tt>x</tt> in the body of the <tt>let</tt> is 3,
so the value of the <tt>let</tt> expression is 33. On the other hand, the
<tt>x</tt> that is the second argument to the outermost <tt>+</tt> is still 5.<p>
<li>The variables' values are computed outside the <tt>let</tt>.
This matters when the expressions that
provide the values for the local variables depend upon
variables having the same names as the local variables themselves.
For example, if the value of <tt>x</tt> is 2, the expression<p>
<p><tt>(let ((x 3)<br>
(y (+ x 2)))<br>
(* x y))<br>
</tt><p>
will have the value 12 because, inside the body of the <tt>let</tt>,
<tt>x</tt> will be 3 and <tt>y</tt> will be 4 (which is the
outer <tt>x</tt> plus 2).
</ul><p><p>
<a name="%_idx_1074"></a><a name="%_idx_1076"></a>Sometimes we can use internal definitions to get the same effect as
with <tt>let</tt>. For example, we could have defined the procedure <tt>f</tt> above as
<p><tt>(define (f x y)<br>
(define a (+ 1 (* x y)))<br>
(define b (- 1 y))<br>
(+ (* x (square a))<br>
(* y b)<br>
(* a b)))<br>
</tt><p>
We prefer, however, to use <tt>let</tt> in situations like this
and to use internal <tt>define</tt> only for internal procedures.<a name="call_footnote_Temp_87" href="#footnote_Temp_87"><sup><small>54</small></sup></a><p>
<p><a name="%_thm_1.34"></a>
<b>Exercise 1.34.</b> Suppose we define the procedure<p>
<p><tt>(define (f g)<br>
(g 2))<br>
</tt><p>
Then we have<p>
<p><tt>(f square)<br>
<i>4</i><br>
<br>
(f (lambda (z) (* z (+ z 1))))<br>
<i>6</i><br>
</tt><p>
What happens if we (perversely) ask the interpreter to evaluate the
combination <tt>(f f)</tt>? Explain.
<p><p>
<a name="%_sec_1.3.3"></a>
<h3><a href="book-Z-H-4.html#%_toc_%_sec_1.3.3">1.3.3 Procedures as General Methods</a></h3><p>
<a name="%_idx_1078"></a><a name="%_idx_1080"></a>
We introduced compound procedures in
section <a href="book-Z-H-10.html#%_sec_1.1.4">1.1.4</a> as a mechanism for abstracting
patterns of numerical operations so as to make them independent of the
particular numbers involved. With higher-order procedures, such as
the <tt>integral</tt> procedure of
section <a href="#%_sec_1.3.1">1.3.1</a>, we began to see a more
powerful kind of abstraction: procedures used to express general
methods of computation, independent of the particular functions
involved. In this section we discuss two more elaborate
examples -- general methods for finding zeros and fixed points of
functions -- and show how these methods can be expressed directly as
procedures.<p>
<a name="%_sec_IGNORE"></a>
<h4><a href="book-Z-H-4.html#%_toc_%_sec_IGNORE">Finding roots of equations by the half-interval method</a></h4><p>
<a name="%_idx_1082"></a>
The <em>half-interval method</em> is a simple but powerful technique for
finding roots of an equation <em>f</em>(<em>x</em>) = 0, where <em>f</em> is a continuous
function. The idea is that, if we are given points <em>a</em> and <em>b</em> such
that <em>f</em>(<em>a</em>) < 0 < <em>f</em>(<em>b</em>), then <em>f</em> must have at least one zero between
<em>a</em> and <em>b</em>. To locate a zero, let <em>x</em> be the average of <em>a</em> and <em>b</em>
and compute <em>f</em>(<em>x</em>). If <em>f</em>(<em>x</em>) > 0, then <em>f</em> must have a zero between
<em>a</em> and <em>x</em>. If <em>f</em>(<em>x</em>) < 0, then <em>f</em> must have a zero between <em>x</em> and
<em>b</em>. Continuing in this way, we can identify smaller and smaller
intervals on which <em>f</em> must have a zero. When we reach a point where
the interval is small enough, the process stops. Since the interval
of uncertainty is reduced by half at each step of the process, the
number of steps required grows as <img src="book-Z-G-D-3.gif">(<tt>log</tt>( <em>L</em>/<em>T</em>)), where <em>L</em> is the
length of the original interval and <em>T</em> is the error tolerance
(that is, the size of the interval we will consider ``small enough'').
Here is a procedure that implements this strategy:<p>
<p><tt><a name="%_idx_1084"></a>(define (search f neg-point pos-point)<br>
(let ((midpoint (average neg-point pos-point)))<br>
(if (close-enough? neg-point pos-point)<br>
midpoint<br>
(let ((test-value (f midpoint)))<br>
(cond ((positive? test-value)<br>
(search f neg-point midpoint))<br>
((negative? test-value)<br>
(search f midpoint pos-point))<br>
(else midpoint))))))<br>
</tt><p><p>
We assume that we are initially given the function <em>f</em> together with
points at which its values are negative and positive. We first
compute the midpoint of the two given points. Next we check to see if
the given interval is small enough, and if so we simply return the
midpoint as our answer. Otherwise, we compute as a test value the
value of <em>f</em> at the midpoint. If the test value is positive, then
we continue the process with a new interval running from the original
negative point to the midpoint. If the test value is negative, we
continue with the interval from the midpoint to the positive point.
Finally, there is the possibility that the test value is 0, in which
case the midpoint is itself the root we are searching for.<p>
To test whether the endpoints are ``close enough'' we can use a
procedure similar to the one used in section <a href="book-Z-H-10.html#%_sec_1.1.7">1.1.7</a> for
computing square roots:<a name="call_footnote_Temp_89" href="#footnote_Temp_89"><sup><small>55</small></sup></a><p>
<p><tt>(define (close-enough? x y)<br>
(< (abs (- x y)) 0.001))<br>
</tt><p><p>
<tt>Search</tt> is awkward to use directly, because
we can accidentally give it points at which <em>f</em>'s
values do not have the required sign, in which case we get a wrong answer.
Instead we will use <tt>search</tt> via the following procedure, which
checks to see which of the endpoints has a negative function value and
which has a positive value, and calls the <tt>search</tt> procedure
accordingly. If the function has the same sign on the two given
points, the half-interval method cannot be used, in which case the
procedure signals an error.<a name="call_footnote_Temp_90" href="#footnote_Temp_90"><sup><small>56</small></sup></a><p>
<p><tt><a name="%_idx_1092"></a>(define (half-interval-method f a b)<br>
(let ((a-value (f a))<br>
(b-value (f b)))<br>
(cond ((and (negative? a-value) (positive? b-value))<br>
(search f a b))<br>
((and (negative? b-value) (positive? a-value))<br>
(search f b a))<br>
(else<br>
(error "Values are not of opposite sign" a b)))))<br>
</tt><p><p>
<a name="%_idx_1094"></a>The following example uses the half-interval method to approximate <img src="book-Z-G-D-9.gif">
as the root between 2 and 4 of <tt>sin</tt> <em>x</em> = 0:<p>
<p><tt>(half-interval-method sin 2.0 4.0)<br>
<i>3.14111328125</i><br>
</tt><p><p>
Here is another example, using the half-interval method
to search for a root of the equation <em>x</em><sup>3</sup> - 2<em>x</em> - 3 = 0
between 1 and 2:<p>
<p><tt>(half-interval-method (lambda (x) (- (* x x x) (* 2 x) 3))<br>
1.0<br>
2.0)<br>
<i>1.89306640625</i><br>
</tt><p><p>
<a name="%_sec_IGNORE"></a>
<h4><a href="book-Z-H-4.html#%_toc_%_sec_IGNORE">Finding fixed points of functions</a></h4><p>
<a name="%_idx_1096"></a><a name="%_idx_1098"></a>
A number <em>x</em> is called a <em>fixed point</em> of a function <em>f</em> if <em>x</em>
satisfies the equation <em>f</em>(<em>x</em>) = <em>x</em>. For some functions <em>f</em> we can locate
a fixed point by beginning with an initial guess and applying <em>f</em>
repeatedly,<p>
<p><div align=left><img src="ch1-Z-G-33.gif"></div><p><p>
until the value does not change very much. Using this idea, we can
devise a procedure <tt>fixed-point</tt> that takes as inputs a function
and an initial guess and produces an approximation to a fixed point of
the function. We apply the function repeatedly until we find two
successive values whose difference is less than some prescribed
tolerance:<p>
<p><tt>(define tolerance 0.00001)<br>
<br>
<a name="%_idx_1100"></a>(define (fixed-point f first-guess)<br>
(define (close-enough? v1 v2)<br>
(< (abs (- v1 v2)) tolerance))<br>
(define (try guess)<br>
(let ((next (f guess)))<br>
(if (close-enough? guess next)<br>
next<br>
(try next))))<br>
(try first-guess))<br>
</tt><p>
<a name="%_idx_1102"></a><a name="%_idx_1104"></a>For example, we can use this method to approximate the fixed point of
the cosine function, starting with 1 as an initial approximation:<a name="call_footnote_Temp_91" href="#footnote_Temp_91"><sup><small>57</small></sup></a><p>
<p><tt><a name="%_idx_1112"></a><a name="%_idx_1114"></a>(fixed-point cos 1.0)<br>
<i>.7390822985224023</i><br>
</tt><p>
Similarly, we can find a solution to the equation
<em>y</em> = <tt>sin</tt> <em>y</em> + <tt>cos</tt> <em>y</em>:<p>
<p><tt><a name="%_idx_1116"></a><a name="%_idx_1118"></a>(fixed-point (lambda (y) (+ (sin y) (cos y)))<br>
1.0)<br>
<i>1.2587315962971173</i><br>
</tt><p><p>
The fixed-point process is reminiscent of the process we used for
finding square roots in section <a href="book-Z-H-10.html#%_sec_1.1.7">1.1.7</a>. Both are based on the
idea of repeatedly improving a guess until the result satisfies some
criterion. In fact, we can readily formulate the <a name="%_idx_1120"></a>square-root
computation as a fixed-point search. Computing the square root of
some number <em>x</em> requires finding a <em>y</em> such that <em>y</em><sup>2</sup> = <em>x</em>. Putting
this equation into the equivalent form <em>y</em> = <em>x</em>/<em>y</em>, we recognize that we
are looking for a fixed point of the function<a name="call_footnote_Temp_92" href="#footnote_Temp_92"><sup><small>58</small></sup></a> <em>y</em> <img src="book-Z-G-D-17.gif"> <em>x</em>/<em>y</em>, and we
can therefore try to compute square roots as<p>
<p><tt>(define (sqrt x)<br>
(fixed-point (lambda (y) (/ x y))<br>
1.0))<br>
</tt><p><p>
Unfortunately, this fixed-point search does not converge. Consider an
initial guess <em>y</em><sub>1</sub>. The next guess is <em>y</em><sub>2</sub> = <em>x</em>/<em>y</em><sub>1</sub> and the next
guess is <em>y</em><sub>3</sub> = <em>x</em>/<em>y</em><sub>2</sub> = <em>x</em>/(<em>x</em>/<em>y</em><sub>1</sub>) = <em>y</em><sub>1</sub>. This results in an infinite
loop in which the two guesses <em>y</em><sub>1</sub> and <em>y</em><sub>2</sub> repeat over and over,
oscillating about the answer.<p>
One way to control such oscillations is to prevent the guesses from
changing so much.
Since the answer is always between our guess <em>y</em>
and <em>x</em>/<em>y</em>, we can make a new guess that is not as far from <em>y</em> as <em>x</em>/<em>y</em>
by averaging <em>y</em> with <em>x</em>/<em>y</em>, so that the next guess after
<em>y</em> is (1/2)(<em>y</em> + <em>x</em>/<em>y</em>) instead of <em>x</em>/<em>y</em>.
The process of making such a sequence of guesses is simply the process
of looking for a fixed point of <em>y</em> <img src="book-Z-G-D-17.gif"> (1/2)(<em>y</em> + <em>x</em>/<em>y</em>):<p>
<p><tt><a name="%_idx_1126"></a>(define (sqrt x)<br>
(fixed-point (lambda (y) (average y (/ x y)))<br>
1.0))<br>
</tt><p>
(Note that <em>y</em> = (1/2)(<em>y</em> + <em>x</em>/<em>y</em>) is a simple transformation of the
equation <em>y</em> = <em>x</em>/<em>y</em>; to derive it, add <em>y</em> to both sides of the equation
and divide by 2.)<p>
With this modification, the square-root procedure works. In fact, if
we unravel the definitions, we can see that the sequence of
approximations to the square root generated here is precisely the
same as the one generated by our original square-root procedure of
section <a href="book-Z-H-10.html#%_sec_1.1.7">1.1.7</a>. This approach of averaging
successive approximations to a solution, a technique we that we call
<a name="%_idx_1128"></a><em>average damping</em>, often aids the convergence of fixed-point
searches.<p>
<p><a name="%_thm_1.35"></a>
<b>Exercise 1.35.</b> <a name="%_idx_1130"></a><a name="%_idx_1132"></a>Show that the golden ratio <img src="book-Z-G-D-11.gif"> (section <a href="book-Z-H-11.html#%_sec_1.2.2">1.2.2</a>)
is a fixed point of the transformation <em>x</em> <img src="book-Z-G-D-17.gif"> 1 + 1/<em>x</em>, and use
this fact to compute <img src="book-Z-G-D-11.gif"> by means of the <tt>fixed-point</tt>
procedure.
<p><p>
<p><a name="%_thm_1.36"></a>
<b>Exercise 1.36.</b> Modify <tt>fixed-point</tt> so that it prints the sequence of
approximations it generates, using
the <tt>newline</tt> and <tt>display</tt> primitives shown in
exercise <a href="book-Z-H-11.html#%_thm_1.22">1.22</a>. Then find a solution to <em>x</em><sup><em>x</em></sup> =
1000 by finding a fixed point of <em>x</em> <img src="book-Z-G-D-17.gif"> <tt>log</tt>(1000)/<tt>log</tt>(<em>x</em>). (Use
Scheme's <a name="%_idx_1134"></a><a name="%_idx_1136"></a>primitive <tt>log</tt> procedure, which computes natural
logarithms.) Compare the number of steps this takes with and without
average damping. (Note that you cannot start <tt>fixed-point</tt> with a
guess of 1, as this would cause division by <tt>log</tt>(1) = 0.)
<p><p>
<p><a name="%_thm_1.37"></a>
<b>Exercise 1.37.</b> <a name="%_idx_1138"></a>a. An infinite <em>continued fraction</em> is an expression of the form
<p><div align=left><img src="ch1-Z-G-34.gif"></div><p>
<a name="%_idx_1140"></a><a name="%_idx_1142"></a>As an example, one can show that the infinite continued fraction
expansion with the <em>N</em><sub><em>i</em></sub> and the <em>D</em><sub><em>i</em></sub> all equal to 1 produces
1/<img src="book-Z-G-D-11.gif">, where <img src="book-Z-G-D-11.gif"> is the golden ratio (described in
section <a href="book-Z-H-11.html#%_sec_1.2.2">1.2.2</a>).
One way to approximate an
infinite continued fraction is to truncate the expansion after a given
number of terms. Such a truncation -- a so-called <em><em>k</em>-term finite
continued fraction</em> -- has the form
<p><div align=left><img src="ch1-Z-G-35.gif"></div><p>
Suppose that <tt>n</tt> and <tt>d</tt> are procedures of one argument (the
term index <em>i</em>) that return the <em>N</em><sub><em>i</em></sub> and <em>D</em><sub><em>i</em></sub> of the terms of the
continued fraction. Define a procedure <tt>cont-frac</tt>
such that evaluating <tt>(cont-frac n d k)</tt>
computes the value of the <em>k</em>-term finite
continued fraction. Check your procedure by approximating 1/<img src="book-Z-G-D-11.gif"> using
<p><tt>(cont-frac (lambda (i) 1.0)<br>
(lambda (i) 1.0)<br>
k)<br>
</tt><p>
for successive values of <tt>k</tt>. How large must you make <tt>k</tt>
in order to get an approximation that is accurate to 4 decimal places?<p>
<p><p>b. If your <tt>cont-frac</tt>
procedure generates a recursive process, write one that generates
an iterative process.
If it generates an iterative process, write one that generates
a recursive process.
<p><p>
<p><a name="%_thm_1.38"></a>
<b>Exercise 1.38.</b> <a name="%_idx_1144"></a>In 1737, the Swiss mathematician Leonhard Euler published a memoir
<em>De Fractionibus Continuis</em>, which included a <a name="%_idx_1146"></a><a name="%_idx_1148"></a>continued fraction
expansion for <em>e</em> - 2, where <em>e</em> is the base of the natural logarithms.
In this fraction, the <em>N</em><sub><em>i</em></sub> are all 1, and the <em>D</em><sub><em>i</em></sub> are successively
1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, <tt>...</tt>. Write a program that uses
your <tt>cont-frac</tt> procedure from
exercise <a href="#%_thm_1.37">1.37</a> to approximate <em>e</em>, based on
Euler's expansion.
<p><p>
<p><a name="%_thm_1.39"></a>
<b>Exercise 1.39.</b> <a name="%_idx_1150"></a><a name="%_idx_1152"></a><a name="%_idx_1154"></a>A continued fraction representation of the tangent function was
published in 1770 by the German mathematician J.H. Lambert:
<p><div align=left><img src="ch1-Z-G-36.gif"></div><p>
where <em>x</em> is in radians.
Define a procedure <tt>(tan-cf x k)</tt> that computes an approximation
to the tangent function based on Lambert's
formula. <tt>K</tt> specifies the number of terms to compute, as in
exercise <a href="#%_thm_1.37">1.37</a>.
<p><p>
<a name="%_sec_1.3.4"></a>
<h3><a href="book-Z-H-4.html#%_toc_%_sec_1.3.4">1.3.4 Procedures as Returned Values</a></h3><p>
<a name="%_idx_1156"></a><a name="%_idx_1158"></a>
The above examples demonstrate how
the ability to pass procedures as arguments significantly enhances
the expressive power of our programming language. We can achieve even
more expressive power by creating procedures whose returned values are
themselves procedures.<p>
We can illustrate this idea by looking again at the fixed-point
example described at the end of
section <a href="#%_sec_1.3.3">1.3.3</a>. We formulated a new version
of the square-root procedure as a fixed-point search, starting with
the observation that <img src="book-Z-G-D-13.gif"><em>x</em> is a fixed-point of the function
<em>y</em> <img src="book-Z-G-D-17.gif"> <em>x</em>/<em>y</em>. Then we used average damping to make the
approximations converge. Average damping is a useful general
technique in itself. Namely, given a function <em>f</em>, we consider the
function whose value at <em>x</em> is equal to the average of <em>x</em> and <em>f</em>(<em>x</em>).<p>
We can express the idea of average damping by means of the
following procedure:<p>
<p><tt><a name="%_idx_1160"></a>(define (average-damp f)<br>
(lambda (x) (average x (f x))))<br>
</tt><p>
<tt>Average-damp</tt> is a procedure that takes as its argument a
procedure <tt>f</tt> and returns as its value a procedure (produced by
the <tt>lambda</tt>) that, when applied to a number <tt>x</tt>, produces the
average of <tt>x</tt> and <tt>(f x)</tt>. For example, applying <tt>average-damp</tt> to the <tt>square</tt> procedure produces a procedure whose
value at some number <em>x</em> is the average of <em>x</em> and <em>x</em><sup>2</sup>. Applying
this resulting procedure to 10 returns the average of 10 and 100, or
55:<a name="call_footnote_Temp_98" href="#footnote_Temp_98"><sup><small>59</small></sup></a><p>
<p><tt>((average-damp square) 10)<br>
<i>55</i><br>
</tt><p><p>
<a name="%_idx_1168"></a>Using <tt>average-damp</tt>, we can reformulate the square-root procedure
as follows:<p>
<p><tt><a name="%_idx_1170"></a>(define (sqrt x)<br>
(fixed-point (average-damp (lambda (y) (/ x y)))<br>
1.0))<br>
</tt><p>
Notice how this formulation makes explicit the three ideas in the
method: fixed-point search, average damping, and the function
<em>y</em> <img src="book-Z-G-D-17.gif"> <em>x</em>/<em>y</em>. It is instructive to compare this formulation of the
square-root method with the original version given in
section <a href="book-Z-H-10.html#%_sec_1.1.7">1.1.7</a>. Bear in mind that these procedures express
the same process, and notice how much clearer the idea becomes when we
express the process in terms of these abstractions. In general, there
are many ways to formulate a process as a procedure. Experienced
programmers know how to choose procedural formulations that are
particularly perspicuous, and where useful elements of the process are
exposed as separate entities that can be reused in other applications.
As a simple example of reuse, notice that the cube root of <em>x</em> is a
fixed point of the function <em>y</em> <img src="book-Z-G-D-17.gif"> <em>x</em>/<em>y</em><sup>2</sup>, so we can immediately
generalize our square-root procedure to one that extracts <a name="%_idx_1172"></a><a name="%_idx_1174"></a>cube
roots:<a name="call_footnote_Temp_99" href="#footnote_Temp_99"><sup><small>60</small></sup></a><p>
<p><tt><a name="%_idx_1176"></a>(define (cube-root x)<br>
(fixed-point (average-damp (lambda (y) (/ x (square y))))<br>
1.0))<br>
</tt><p><p>
<a name="%_sec_IGNORE"></a>
<h4><a href="book-Z-H-4.html#%_toc_%_sec_IGNORE">Newton's method</a></h4><p>
<a name="%_idx_1178"></a>
When we first introduced the square-root procedure, in
section <a href="book-Z-H-10.html#%_sec_1.1.7">1.1.7</a>, we mentioned that this was a special case of
<em>Newton's method</em>.
If <em>x</em> <img src="book-Z-G-D-17.gif"> <em>g</em>(<em>x</em>) is a differentiable function, then a solution of
the equation <em>g</em>(<em>x</em>) = 0 is a fixed point of the function <em>x</em> <img src="book-Z-G-D-17.gif"> <em>f</em>(<em>x</em>)
where
<p><div align=left><img src="ch1-Z-G-37.gif"></div><p>
and
<em>D</em><em>g</em>(<em>x</em>) is the derivative of <em>g</em> evaluated at <em>x</em>. <a name="%_idx_1180"></a>Newton's
method is the use of the fixed-point method we saw above to
approximate a solution of the equation by finding a fixed point of
the function <em>f</em>.<a name="call_footnote_Temp_100" href="#footnote_Temp_100"><sup><small>61</small></sup></a>
For many functions <em>g</em> and for sufficiently good initial guesses for
<em>x</em>, Newton's method converges very rapidly to a solution of
<em>g</em>(<em>x</em>) = 0.<a name="call_footnote_Temp_101" href="#footnote_Temp_101"><sup><small>62</small></sup></a><p>
<a name="%_idx_1186"></a><a name="%_idx_1188"></a><a name="%_idx_1190"></a>In order to implement Newton's method as a procedure, we must first
express the idea of derivative. Note that ``derivative,'' like
average damping, is something that transforms a function into another
function. For instance, the derivative of the function <em>x</em> <img src="book-Z-G-D-17.gif">
<em>x</em><sup>3</sup> is the function <em>x</em> <img src="book-Z-G-D-17.gif"> 3<em>x</em><sup>2</sup>. In general, if <em>g</em> is a
function and <em>d</em><em>x</em> is a small number, then the derivative <em>D</em><em>g</em> of <em>g</em> is
the function whose value at any number <em>x</em> is given (in the limit of
small <em>d</em><em>x</em>) by
<p><div align=left><img src="ch1-Z-G-38.gif"></div><p>
Thus, we can express the idea of derivative (taking <em>d</em><em>x</em> to be, say,
0.00001) as the procedure<p>
<p><tt><a name="%_idx_1192"></a>(define (deriv g)<br>
(lambda (x)<br>
(/ (- (g (+ x dx)) (g x))<br>
dx)))<br>
</tt><p>
along with the definition<p>
<p><tt>(define dx 0.00001)<br>
</tt><p><p>
Like <tt>average-damp</tt>, <tt>deriv</tt> is a procedure that takes a
procedure as argument and returns a procedure as value. For example,
to approximate the derivative of <em>x</em> <img src="book-Z-G-D-17.gif"> <em>x</em><sup>3</sup> at 5 (whose exact
value is 75) we can evaluate<p>
<p><tt><a name="%_idx_1194"></a>(define (cube x) (* x x x))<br>
<br>
((deriv cube) 5)<br>
<i>75.00014999664018</i><br>
</tt><p><p>
With the aid of <tt>deriv</tt>, we can express Newton's method as a
fixed-point process:<p>
<p><tt><a name="%_idx_1196"></a>(define (newton-transform g)<br>
(lambda (x)<br>
(- x (/ (g x) ((deriv g) x)))))<br>
<br>
<a name="%_idx_1198"></a>(define (newtons-method g guess)<br>
(fixed-point (newton-transform g) guess))<br>
</tt><p>
The <tt>newton-transform</tt> procedure expresses the formula at the
beginning of this section, and <tt>newtons-method</tt> is readily defined
in terms of this. It takes as arguments a procedure that computes the
function for which we want to find a zero, together with an initial
guess. For instance, to find the <a name="%_idx_1200"></a>square root of <em>x</em>, we can use
Newton's method to find a zero of the function <em>y</em> <img src="book-Z-G-D-17.gif"> <em>y</em><sup>2</sup> - <em>x</em> starting with
an initial guess of 1.<a name="call_footnote_Temp_102" href="#footnote_Temp_102"><sup><small>63</small></sup></a>
This provides yet another form of the square-root
procedure:<p>
<p><tt><a name="%_idx_1202"></a>(define (sqrt x)<br>
(newtons-method (lambda (y) (- (square y) x))<br>
1.0))<br>
</tt><p><p>
<a name="%_sec_IGNORE"></a>
<h4><a href="book-Z-H-4.html#%_toc_%_sec_IGNORE">Abstractions and first-class procedures</a></h4><p>
We've seen two ways to express the square-root
computation as an instance of a more general method, once as a fixed-point
search and once using Newton's method. Since Newton's method
was itself expressed as a fixed-point process,
we actually saw two ways to compute square roots as fixed points.
Each method begins with a function and finds a <a name="%_idx_1204"></a>fixed
point of some transformation of the function. We can express this
general idea itself as a procedure:<p>
<p><tt><a name="%_idx_1206"></a>(define (fixed-point-of-transform g transform guess)<br>
(fixed-point (transform g) guess))<br>
</tt><p>
This very general procedure takes as its arguments a procedure <tt>g</tt>
that computes some function, a procedure that transforms <tt>g</tt>, and
an initial guess. The returned result is a fixed point of the
transformed function.<p>
<a name="%_idx_1208"></a>Using this abstraction, we can recast the first square-root
computation from this section (where we look for
a fixed point of the average-damped version of <em>y</em> <img src="book-Z-G-D-17.gif"> <em>x</em>/<em>y</em>)
as an instance of this general method:<p>
<p><tt><a name="%_idx_1210"></a>(define (sqrt x)<br>
(fixed-point-of-transform (lambda (y) (/ x y))<br>
average-damp<br>
1.0))<br>
</tt><p>
<a name="%_idx_1212"></a>Similarly, we can express the second square-root computation from this section
(an instance
of Newton's method that finds a fixed point of the
Newton transform of <em>y</em> <img src="book-Z-G-D-17.gif"> <em>y</em><sup>2</sup> - <em>x</em>) as<p>
<p><tt><a name="%_idx_1214"></a><a name="%_idx_1216"></a>(define (sqrt x)<br>
(fixed-point-of-transform (lambda (y) (- (square y) x))<br>
newton-transform<br>
1.0))<br>
</tt><p><p>