From 4f17ed85b08cd06e537167e544fad0dba982eb11 Mon Sep 17 00:00:00 2001 From: Darcy Shen Date: Thu, 11 Apr 2024 17:22:26 +0800 Subject: [PATCH] SICP 07: slides and code snippets --- SICP/code07.tm | 333 +++++++++++++++++++++++++++++++++++++++++ SICP/index.tm | 8 + SICP/slide07.tm | 387 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 728 insertions(+) create mode 100644 SICP/code07.tm create mode 100644 SICP/slide07.tm diff --git a/SICP/code07.tm b/SICP/code07.tm new file mode 100644 index 0000000..d3ee7f5 --- /dev/null +++ b/SICP/code07.tm @@ -0,0 +1,333 @@ + + +> + +<\body> + + + <\session|scheme|default> + <\unfolded-io|Scheme] > + (define (cube x) (* x x x)) + <|unfolded-io> + cube + + + <\unfolded-io|Scheme] > + (define (sum-integers a b) + + \ \ (if (\ a b) + + \ \ \ \ \ \ 0 + + \ \ \ \ \ \ (+ a (sum-integers (+ a 1) b)))) + <|unfolded-io> + sum-integers + + + <\unfolded-io|Scheme] > + (define (sum-cubes a b) + + \ \ (if (\ a b) + + \ \ \ \ \ \ 0 + + \ \ \ \ \ \ (+ (cube a) (sum-cubes (+ a 1) b)))) + <|unfolded-io> + sum-cubes + + + <\unfolded-io|Scheme] > + (define (pi-sum a b) + + \ \ (if (\ a b) + + \ \ \ \ \ \ 0 + + \ \ \ \ \ \ (+ (/ 1.0 (* a (+ a 2))) (pi-sum (+ a 4) b)))) + <|unfolded-io> + pi-sum + + + <\unfolded-io|Scheme] > + (sum-integers 1 3) + <|unfolded-io> + 6 + + + <\unfolded-io|Scheme] > + (sum-cubes 1 3) + <|unfolded-io> + 36 + + + <\unfolded-io|Scheme] > + (pi-sum 1 3) + <|unfolded-io> + 0.3333333333333333 + + + <\unfolded-io|Scheme] > + (define (sum term a next b) + + \ \ (if (\ a b) + + \ \ \ \ \ \ 0 + + \ \ \ \ \ \ (+ (term a) + + \ \ \ \ \ \ \ \ \ (sum term (next a) next b)))) + <|unfolded-io> + sum + + + <\unfolded-io|Scheme] > + (define (inc n) (+ n 1)) + <|unfolded-io> + inc + + + <\unfolded-io|Scheme] > + (define (sum-cubes-2 a b) + + \ \ (sum cube a inc b)) + <|unfolded-io> + sum-cubes-2 + + + <\unfolded-io|Scheme] > + (sum-cubes-2 1 3) + <|unfolded-io> + 36 + + + <\input|Scheme] > + \; + + + <\unfolded-io|Scheme] > + (define (identity x) x) + <|unfolded-io> + identity + + + <\unfolded-io|Scheme] > + (define (sum-integers-2 a b) + + \ \ (sum identity a inc b)) + <|unfolded-io> + sum-integers-2 + + + <\unfolded-io|Scheme] > + (sum-integers-2 1 10) + <|unfolded-io> + 55 + + + <\unfolded-io|Scheme] > + (define (pi-sum-2 a b) + + \ \ (define (pi-term x) + + \ \ \ \ (/ 1.0 (* x (+ x 2)))) + + \ \ (define (pi-next x) + + \ \ \ \ (+ x 4)) + + \ \ (sum pi-term a pi-next b)) + <|unfolded-io> + pi-sum-2 + + + <\unfolded-io|Scheme] > + (* 8 (pi-sum-2 1 1000)) + <|unfolded-io> + 3.139592655589783 + + + <\input|Scheme] > + \; + + + + + + <\session|scheme|default> + <\unfolded-io|Scheme] > + (define (square x) (* x x)) + <|unfolded-io> + square + + + <\unfolded-io|Scheme] > + ((lambda (x y z) (+ x y (square z))) 1 2 3) + <|unfolded-io> + 12 + + + <\input|Scheme] > + \; + + + + + + <\session|scheme|default> + <\unfolded-io|Scheme] > + (define (average x y) (/ (+ x y) 2)) + <|unfolded-io> + average + + + <\unfolded-io|Scheme] > + (define (search f neg-point pos-point) + + \ \ (let ((midpoint (average neg-point pos-point))) + + \ \ \ \ (if (close-enough? neg-point pos-point) + + \ \ \ \ \ \ \ \ midpoint + + \ \ \ \ \ \ \ \ (let ((test-value (f midpoint))) + + \ \ \ \ \ \ \ \ \ \ (cond ((positive? test-value) + + \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (search f neg-point midpoint)) + + \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ((negative? test-value) + + \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (search f midpoint pos-point)) + + \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (else midpoint)))))) + <|unfolded-io> + search + + + <\unfolded-io|Scheme] > + (define (close-enough? x y) + + \ \ (\ (abs (- x y)) 0.001)) + <|unfolded-io> + close-enough? + + + <\unfolded-io|Scheme] > + (define (half-interval-method f a b) + + \ \ (let ((a-value (f a)) + + \ \ \ \ \ \ \ \ (b-value (f b))) + + \ \ \ \ (cond ((and (negative? a-value) (positive? b-value)) + + \ \ \ \ \ \ \ \ \ \ \ (search f a b)) + + \ \ \ \ \ \ \ \ \ \ ((and (negative? b-value) (positive? a-value)) + + \ \ \ \ \ \ \ \ \ \ \ (search f b a)) + + \ \ \ \ \ \ \ \ \ \ (else + + \ \ \ \ \ \ \ \ \ \ \ (error "Values are not of opposite sign" a b))))) + <|unfolded-io> + half-interval-method + + + <\unfolded-io|Scheme] > + (half-interval-method sin 2.0 4.0) + <|unfolded-io> + 3.14111328125 + + + <\unfolded-io|Scheme] > + (half-interval-method (lambda (x) (- (* x x x) (* 2 x) 3)) + + \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 1.0 + + \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 2.0) + <|unfolded-io> + 1.89306640625 + + + <\input|Scheme] > + \; + + + <\unfolded-io|Scheme] > + (define tolerance 0.00001) + <|unfolded-io> + 0.00001 + + + <\unfolded-io|Scheme] > + (define (fixed-point f first-guess) + + \ \ (define (close-enough? v1 v2) + + \ \ \ \ (\ (abs (- v1 v2)) tolerance)) + + \ \ (define (try guess) + + \ \ \ \ (let ((next (f guess))) + + \ \ \ \ \ \ (if (close-enough? guess next) + + \ \ \ \ \ \ \ \ \ \ next + + \ \ \ \ \ \ \ \ \ \ (try next)))) + + \ \ (try first-guess)) + <|unfolded-io> + fixed-point + + + <\unfolded-io|Scheme] > + (fixed-point cos 1.0) + <|unfolded-io> + 0.7390822985224024 + + + <\unfolded-io|Scheme] > + (fixed-point (lambda (y) (+ (sin y) (cos y))) + + \ \ \ \ \ \ \ \ \ \ \ \ \ 1.0) + <|unfolded-io> + 1.2587315962971173 + + + <\input|Scheme] > + \; + + + + +<\initial> + <\collection> + + + + + +<\references> + <\collection> + > + > + > + + + +<\auxiliary> + <\collection> + <\associate|toc> + |1.3.1 |.>>>>|> + > + + |1.3.2 |.>>>>|> + > + + |1.3.3 |.>>>>|> + > + + + \ No newline at end of file diff --git a/SICP/index.tm b/SICP/index.tm index 9e9ce4c..d23bf70 100644 --- a/SICP/index.tm +++ b/SICP/index.tm @@ -24,6 +24,14 @@ \<#76EE\>\<#6807\>\<#90AE\>\<#7BB1\>\<#FF1A\>shenda AT ustc.edu + \<#7B2C\>7\<#8BFE\>\<#FF1A\>\<#9AD8\>\<#9636\>\<#51FD\>\<#6570\> + + \<#7EC3\>\<#4E60\>\<#FF1A\>\<#8BFE\>\<#540E\>\<#4E60\>\<#9898\>\<#7684\>\<#6570\>\<#5B66\>\<#542B\>\<#91CF\>\<#6BD4\>\<#8F83\>\<#91CD\>\<#FF0C\>\<#53EF\>\<#81EA\>\<#884C\>\<#9009\>\<#62E9\>\<#4E00\>\<#4E9B\>\<#4E60\>\<#9898\>\<#7EC3\>\<#4E60\>\<#4E00\>\<#4E0B\> + + \<#8BFE\>\<#4EF6\>\<#FF1A\>\<#706F\>\<#7247\>07|slide07.tm> + + \<#4EE3\>\<#7801\>\<#FF1A\>\<#7801\>\<#6E05\>\<#5355\>07|code07.tm> + \<#7B2C\>6\<#8BFE\>\<#FF1A\>\<#7B97\>\<#6CD5\>\<#7684\>\<#65F6\>\<#7A7A\>\<#590D\>\<#6742\>\<#5EA6\>\<#FF08\>\<#4E60\>\<#9898\>\<#8BFE\>\<#FF09\> \<#7EC3\>\<#4E60\>\<#FF1A\>\<#9898\>\<#8BFE\>\<#8BFE\>\<#4EF6\>06|https://gitee.com/XmacsLabs/interactive-sicp/raw/main/exercise06.tm> diff --git a/SICP/slide07.tm b/SICP/slide07.tm new file mode 100644 index 0000000..c2c72ca --- /dev/null +++ b/SICP/slide07.tm @@ -0,0 +1,387 @@ + + +> + +<\body> + <\hide-preamble> + >>> + + + <\slideshow> + <\slide> + \; + + \; + + \; + + \; + + \<#57FA\>\<#7840\>SICP + 07>|>>|\<#6D6A\>\<#718A\>\<#732B\>\<#513F\>>>>|>>|\<#9AD8\>\<#9636\>\<#51FD\>\<#6570\>\<#505A\>\<#62BD\>\<#8C61\>>> + + + <\slide> + \<#57FA\>\<#7840\>SICP\<#FF1A\>\<#7B2C\>7\<#8BFE\>> + + <\wide-tabular> + || + + \<#7528\>\<#9AD8\>\<#9636\>\<#51FD\>\<#6570\>\<#505A\>\<#62BD\>\<#8C61\> + > + |<\cell> + <\very-large> + + + >| + \<#5728\>\<#53C2\>\<#6570\>\<#4E2D\>\<#4F20\>\<#9012\>\<#51FD\>\<#6570\>\<#503C\> + |<\cell> + Procedures as Arguments + >| + \<#7528\>\<#6784\>\<#9020\>\<#533F\>\<#540D\>\<#51FD\>\<#6570\> + |<\cell> + Constructing Procedures Using + >| + \<#51FD\>\<#6570\>\<#4F5C\>\<#4E3A\>\<#4E00\>\<#822C\>\<#6027\>\<#7684\>\<#65B9\>\<#6CD5\> + |<\cell> + Procedures as General Methods + >| + \<#5728\>\<#51FD\>\<#6570\>\<#4E2D\>\<#8FD4\>\<#56DE\>\<#51FD\>\<#6570\>\<#503C\> + |<\cell> + Procedures as Returned Values + >>> + + + \; + + \<#7A0B\>\<#7684\>\<#57FA\>\<#672C\>\<#539F\>\<#7406\>\<#662F\>\<#5BF9\>\<#6570\>\<#636E\>\<#548C\>\<#8BA1\>\<#7B97\>\<#7684\>\<#7EC4\>\<#5408\>\<#548C\>\<#62BD\>\<#8C61\>> + + + <\slide> + \<#53C2\>\<#6570\>\<#4E2D\>\<#4F20\>\<#9012\>\<#51FD\>\<#6570\>\<#503C\>> + + <\with|par-columns|2> + <\equation*> + f=f+\+f + + + <\equation*> + i=1+2+\+100=5050 + + + <\equation*> + lim+\>>=|8> + + + <\session|scheme|default> + <\folded-io|Scheme] > + (define (sum f a next b) + + \ \ (if (\ a b) + + \ \ \ \ \ \ 0 + + \ \ \ \ \ \ (+ (f a) + + \ \ \ \ \ \ \ \ \ (sum f (next a) next b)))) + <|folded-io> + sum + + + <\folded-io|Scheme] > + (define (inc n) (+ n 1)) + <|folded-io> + inc + + + <\folded-io|Scheme] > + (define (same x) x) + <|folded-io> + same + + + <\folded-io|Scheme] > + \; + <|folded-io> + 5050 + + + + + <\session|scheme|default> + <\unfolded-io|Scheme] > + (sum 1 100) + <|unfolded-io> + 5050 + + + <\input|Scheme] > + \; + + + + \; + + + <\slide> + \<#6784\>\<#9020\>\<#533F\>\<#540D\>\<#51FD\>\<#6570\>> + + <\equation*> + i=1+2+\+10 + + + \; + + <\session|scheme|default> + <\folded-io|Scheme] > + (sum (lambda (x) (* x x)) 1 10) + <|folded-io> + 385 + + + <\input|Scheme] > + \; + + + + \; + + <\equation*> + 1+3+5+7+9 + + + <\session|scheme|default> + <\folded-io|Scheme] > + (sum (lambda (x) (* x x)) 1 (lambda (x) (+ x 2)) 10) + <|folded-io> + 165 + + + <\unfolded-io|Scheme] > + (sum (lambda (x) (* x x)) 2 (lambda (x) (+ x 2)) 10) + <|unfolded-io> + 220 + + + <\input|Scheme] > + \; + + + + + <\slide> + \<#521B\>\<#5EFA\>\<#5C40\>\<#90E8\>\<#53D8\>\<#91CF\>> + + <\session|scheme|default> + <\unfolded-io|Scheme] > + (define (fib-once pair) + + \ \ (let ((a (first pair)) + + \ \ \ \ \ \ \ \ (b (second pair))) + + \ \ \ (list b (+ a b)))) + <|unfolded-io> + fib-once + + + <\unfolded-io|Scheme] > + (fib-once (fib-once (list 1 1))) + <|unfolded-io> + (2 3) + + + <\input|Scheme] > + \; + + + + \; + + + <\slide> + \<#4F8B\>\<#5206\>\<#6790\>\<#FF1A\>\<#533A\>\<#95F4\>\<#6298\>\<#534A\>\<#6CD5\>> + + <\gr-screen> + |gr-frame|>|gr-geometry||gr-grid||1>|gr-grid-old||1>|gr-edit-grid-aspect|||>|gr-edit-grid||1>|gr-edit-grid-old||1>|gr-color|magenta|||||>|||>>||||>|>||>|||>|||>||>>||>>||>>||>>>> + + + + <\slide> + \<#73B0\>\<#FF1A\>\<#4E8C\>\<#5206\>\<#67E5\>\<#627E\>\<#6CD5\>> + + <\session|scheme|default> + <\unfolded-io|Scheme] > + (define (average x y) (/ (+ x y) 2)) + <|unfolded-io> + average + + + <\unfolded-io|Scheme] > + (define (search f neg-point pos-point) + + \ \ (let ((midpoint (average neg-point pos-point))) + + \ \ \ \ (if (close-enough? neg-point pos-point) + + \ \ \ \ \ \ \ \ midpoint + + \ \ \ \ \ \ \ \ (let ((test-value (f midpoint))) + + \ \ \ \ \ \ \ \ \ \ (cond ((positive? test-value) + + \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (search f neg-point midpoint)) + + \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ((negative? test-value) + + \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (search f midpoint pos-point)) + + \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (else midpoint)))))) + <|unfolded-io> + search + + + + + <\slide> + \<#73B0\>\<#FF1A\>\<#533A\>\<#95F4\>\<#6298\>\<#534A\>\<#6CD5\>> + + <\session|scheme|default> + <\folded-io|Scheme] > + (define (close-enough? x y) + + \ \ (\ (abs (- x y)) 0.001)) + <|folded-io> + close-enough? + + + <\folded-io|Scheme] > + (define (half-interval-method f a b) + + \ \ (let ((a-value (f a)) + + \ \ \ \ \ \ \ \ (b-value (f b))) + + \ \ \ \ (cond ((and (negative? a-value) (positive? b-value)) + + \ \ \ \ \ \ \ \ \ \ \ (search f a b)) + + \ \ \ \ \ \ \ \ \ \ ((and (negative? b-value) (positive? a-value)) + + \ \ \ \ \ \ \ \ \ \ \ (search f b a)) + + \ \ \ \ \ \ \ \ \ \ (else + + \ \ \ \ \ \ \ \ \ \ \ (error "Values are not of opposite sign" a + b))))) + <|folded-io> + half-interval-method + + + <\unfolded-io|Scheme] > + (half-interval-method sin 2.0 4.0) + <|unfolded-io> + 3.14111328125 + + + + + <\slide> + \<#4F8B\>\<#5206\>\<#6790\>\<#FF1A\>\<#5BFB\>\<#627E\>\<#4E0D\>\<#52A8\>\<#70B9\>> + + <\equation*> + f,f|)>,f|)>|)>,\ + + + <\session|scheme|default> + <\folded-io|Scheme] > + (define tolerance 0.00001) + <|folded-io> + 0.00001 + + + <\folded-io|Scheme] > + (define (fixed-point f first-guess) + + \ \ (define (close-enough? v1 v2) + + \ \ \ \ (\ (abs (- v1 v2)) tolerance)) + + \ \ (define (try guess) + + \ \ \ \ (let ((next (f guess))) + + \ \ \ \ \ \ (if (close-enough? guess next) + + \ \ \ \ \ \ \ \ \ \ next + + \ \ \ \ \ \ \ \ \ \ (try next)))) + + \ \ (try first-guess)) + <|folded-io> + fixed-point + + + <\folded-io|Scheme] > + (fixed-point cos 1.0) + <|folded-io> + 0.7390822985224024 + + + <\folded-io|Scheme] > + (fixed-point (lambda (y) (+ (sin y) (cos y))) 1.0) + <|folded-io> + 1.2587315962971173 + + + + + <\slide> + \<#7ED3\>> + + \<#4EE3\>\<#7801\>\<#5373\>\<#6570\>\<#636E\>\<#FF0C\>\<#6570\>\<#636E\>\<#5373\>\<#4EE3\>\<#7801\>\<#3002\> + + \<#51FD\>\<#6570\>\<#503C\>\<#53EF\>\<#4EE5\>\<#50CF\>\<#6570\>\<#636E\>\<#4E00\>\<#6837\>\<#FF0C\>\<#4F5C\>\<#4E3A\>\<#53C2\>\<#6570\>\<#4F20\>\<#5165\>\<#51FD\>\<#6570\>\<#FF0C\>\<#4F5C\>\<#4E3A\>\<#8FD4\>\<#56DE\>\<#503C\>\<#4ECE\>\<#51FD\>\<#6570\>\<#4E2D\>\<#4F20\>\<#51FA\>\<#3002\> + + \<#FF0C\>\<#FF0C\> + + <\scm-code> + (define (\<#51FD\>\<#6570\>\<#540D\>\ + >\<#5F62\>\<#5F0F\>\<#53C2\>\<#6570\>\<#5217\>\<#8868\>\>) + \<#4E3B\>\<#4F53\>\>) + + (lambda (\<#5F62\>\<#5F0F\>\<#53C2\>\<#6570\>\<#5217\>\<#8868\>\>) + \<#4E3B\>\<#4F53\>\>) + + \; + + (let ((\<#53D8\>\<#91CF\>1\> + \<#8868\>\<#8FBE\>\<#5F0F\>1\>) + + \ \ \ \ \ \ (\<#53D8\>\<#91CF\>2\> + \<#8868\>\<#8FBE\>\<#5F0F\>2\>) + + \ \ \ \ \ \ ... + + \ \ \ \ \ \ (\<#53D8\>\<#91CF\>n\> + \<#8868\>\<#8FBE\>\<#5F0F\>n\>)) + + \ \ \ \<#4E3B\>\<#4F53\>\>) + + + + + +<\initial> + <\collection> + + + + + + + + + + + \ No newline at end of file