mirror of https://gitee.com/XmacsLabs/planet.git
SICP 05/07: use S7 Scheme session
This commit is contained in:
parent
a8150764c3
commit
5fb52afc54
|
@ -1,12 +1,14 @@
|
||||||
<TeXmacs|2.1.2>
|
<TeXmacs|2.1.2>
|
||||||
|
|
||||||
<style|<tuple|generic|no-page-numbers|british|reduced-margins>>
|
<style|<tuple|generic|no-page-numbers|british|reduced-margins|s7>>
|
||||||
|
|
||||||
<\body>
|
<\body>
|
||||||
<section*|Orders of Growth>
|
<section*|Orders of Growth>
|
||||||
|
|
||||||
<\session|scheme|default>
|
<\session|s7|default>
|
||||||
<\unfolded-io|Scheme] >
|
<\unfolded-io>
|
||||||
|
\<gtr\>\
|
||||||
|
<|unfolded-io>
|
||||||
(define (expt-1 b n)
|
(define (expt-1 b n)
|
||||||
|
|
||||||
\ \ (if (= n 0)
|
\ \ (if (= n 0)
|
||||||
|
@ -18,19 +20,25 @@
|
||||||
expt-1
|
expt-1
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
<\unfolded-io|Scheme] >
|
<\unfolded-io>
|
||||||
|
\<gtr\>\
|
||||||
|
<|unfolded-io>
|
||||||
(expt-1 1 0)
|
(expt-1 1 0)
|
||||||
<|unfolded-io>
|
<|unfolded-io>
|
||||||
1
|
1
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
<\unfolded-io|Scheme] >
|
<\unfolded-io>
|
||||||
|
\<gtr\>\
|
||||||
|
<|unfolded-io>
|
||||||
(expt-1 2 3)
|
(expt-1 2 3)
|
||||||
<|unfolded-io>
|
<|unfolded-io>
|
||||||
8
|
8
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
<\unfolded-io|Scheme] >
|
<\unfolded-io>
|
||||||
|
\<gtr\>\
|
||||||
|
<|unfolded-io>
|
||||||
(define (expt-2 b n)
|
(define (expt-2 b n)
|
||||||
|
|
||||||
\ \ (define (expt-iter b counter product)
|
\ \ (define (expt-iter b counter product)
|
||||||
|
@ -52,21 +60,27 @@
|
||||||
expt-2
|
expt-2
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
<\unfolded-io|Scheme] >
|
<\unfolded-io>
|
||||||
|
\<gtr\>\
|
||||||
|
<|unfolded-io>
|
||||||
(expt-2 3 3)
|
(expt-2 3 3)
|
||||||
<|unfolded-io>
|
<|unfolded-io>
|
||||||
27
|
27
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
<\input|Scheme] >
|
<\input>
|
||||||
|
\<gtr\>\
|
||||||
|
<|input>
|
||||||
\;
|
\;
|
||||||
</input>
|
</input>
|
||||||
</session>
|
</session>
|
||||||
|
|
||||||
<section*|Exponentiation>
|
<section*|Exponentiation>
|
||||||
|
|
||||||
<\session|scheme|default>
|
<\session|s7|default>
|
||||||
<\unfolded-io|Scheme] >
|
<\unfolded-io>
|
||||||
|
\<gtr\>\
|
||||||
|
<|unfolded-io>
|
||||||
(define (fast-expt b n)
|
(define (fast-expt b n)
|
||||||
|
|
||||||
\ \ (define (even? n)
|
\ \ (define (even? n)
|
||||||
|
@ -86,17 +100,27 @@
|
||||||
fast-expt
|
fast-expt
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
<\unfolded-io|Scheme] >
|
<\unfolded-io>
|
||||||
|
\<gtr\>\
|
||||||
|
<|unfolded-io>
|
||||||
(fast-expt 3 3)
|
(fast-expt 3 3)
|
||||||
<|unfolded-io>
|
<|unfolded-io>
|
||||||
27
|
27
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
|
<\input>
|
||||||
|
\<gtr\>\
|
||||||
|
<|input>
|
||||||
|
\;
|
||||||
|
</input>
|
||||||
</session>
|
</session>
|
||||||
|
|
||||||
<section*|Greatest Common Divisors>
|
<section*|Greatest Common Divisors>
|
||||||
|
|
||||||
<\session|scheme|default>
|
<\session|s7|default>
|
||||||
<\unfolded-io|Scheme] >
|
<\unfolded-io>
|
||||||
|
\<gtr\>\
|
||||||
|
<|unfolded-io>
|
||||||
(define (gcd a b)
|
(define (gcd a b)
|
||||||
|
|
||||||
\ \ (if (= b 0)
|
\ \ (if (= b 0)
|
||||||
|
@ -108,11 +132,19 @@
|
||||||
gcd
|
gcd
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
<\unfolded-io|Scheme] >
|
<\unfolded-io>
|
||||||
|
\<gtr\>\
|
||||||
|
<|unfolded-io>
|
||||||
(gcd 10 20)
|
(gcd 10 20)
|
||||||
<|unfolded-io>
|
<|unfolded-io>
|
||||||
10
|
10
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
|
<\input>
|
||||||
|
\<gtr\>\
|
||||||
|
<|input>
|
||||||
|
\;
|
||||||
|
</input>
|
||||||
</session>
|
</session>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
|
@ -128,4 +160,22 @@
|
||||||
<associate|auto-2|<tuple|?|?>>
|
<associate|auto-2|<tuple|?|?>>
|
||||||
<associate|auto-3|<tuple|?|?>>
|
<associate|auto-3|<tuple|?|?>>
|
||||||
</collection>
|
</collection>
|
||||||
</references>
|
</references>
|
||||||
|
|
||||||
|
<\auxiliary>
|
||||||
|
<\collection>
|
||||||
|
<\associate|toc>
|
||||||
|
<vspace*|1fn><with|font-series|<quote|bold>|math-font-series|<quote|bold>|Orders
|
||||||
|
of Growth> <datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>>
|
||||||
|
<no-break><pageref|auto-1><vspace|0.5fn>
|
||||||
|
|
||||||
|
<vspace*|1fn><with|font-series|<quote|bold>|math-font-series|<quote|bold>|Exponentiation>
|
||||||
|
<datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>>
|
||||||
|
<no-break><pageref|auto-2><vspace|0.5fn>
|
||||||
|
|
||||||
|
<vspace*|1fn><with|font-series|<quote|bold>|math-font-series|<quote|bold>|Greatest
|
||||||
|
Common Divisors> <datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>>
|
||||||
|
<no-break><pageref|auto-3><vspace|0.5fn>
|
||||||
|
</associate>
|
||||||
|
</collection>
|
||||||
|
</auxiliary>
|
140
SICP/code07.tm
140
SICP/code07.tm
|
@ -1,18 +1,22 @@
|
||||||
<TeXmacs|2.1.2>
|
<TeXmacs|2.1.2>
|
||||||
|
|
||||||
<style|<tuple|generic|no-page-numbers|chinese>>
|
<style|<tuple|generic|no-page-numbers|chinese|s7>>
|
||||||
|
|
||||||
<\body>
|
<\body>
|
||||||
<subsection*|1.3.1>
|
<subsection*|1.3.1>
|
||||||
|
|
||||||
<\session|scheme|default>
|
<\session|s7|default>
|
||||||
<\unfolded-io|Scheme] >
|
<\unfolded-io>
|
||||||
|
\<gtr\>\
|
||||||
|
<|unfolded-io>
|
||||||
(define (cube x) (* x x x))
|
(define (cube x) (* x x x))
|
||||||
<|unfolded-io>
|
<|unfolded-io>
|
||||||
cube
|
cube
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
<\unfolded-io|Scheme] >
|
<\unfolded-io>
|
||||||
|
\<gtr\>\
|
||||||
|
<|unfolded-io>
|
||||||
(define (sum-integers a b)
|
(define (sum-integers a b)
|
||||||
|
|
||||||
\ \ (if (\<gtr\> a b)
|
\ \ (if (\<gtr\> a b)
|
||||||
|
@ -24,7 +28,9 @@
|
||||||
sum-integers
|
sum-integers
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
<\unfolded-io|Scheme] >
|
<\unfolded-io>
|
||||||
|
\<gtr\>\
|
||||||
|
<|unfolded-io>
|
||||||
(define (sum-cubes a b)
|
(define (sum-cubes a b)
|
||||||
|
|
||||||
\ \ (if (\<gtr\> a b)
|
\ \ (if (\<gtr\> a b)
|
||||||
|
@ -36,7 +42,9 @@
|
||||||
sum-cubes
|
sum-cubes
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
<\unfolded-io|Scheme] >
|
<\unfolded-io>
|
||||||
|
\<gtr\>\
|
||||||
|
<|unfolded-io>
|
||||||
(define (pi-sum a b)
|
(define (pi-sum a b)
|
||||||
|
|
||||||
\ \ (if (\<gtr\> a b)
|
\ \ (if (\<gtr\> a b)
|
||||||
|
@ -48,25 +56,33 @@
|
||||||
pi-sum
|
pi-sum
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
<\unfolded-io|Scheme] >
|
<\unfolded-io>
|
||||||
|
\<gtr\>\
|
||||||
|
<|unfolded-io>
|
||||||
(sum-integers 1 3)
|
(sum-integers 1 3)
|
||||||
<|unfolded-io>
|
<|unfolded-io>
|
||||||
6
|
6
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
<\unfolded-io|Scheme] >
|
<\unfolded-io>
|
||||||
|
\<gtr\>\
|
||||||
|
<|unfolded-io>
|
||||||
(sum-cubes 1 3)
|
(sum-cubes 1 3)
|
||||||
<|unfolded-io>
|
<|unfolded-io>
|
||||||
36
|
36
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
<\unfolded-io|Scheme] >
|
<\unfolded-io>
|
||||||
|
\<gtr\>\
|
||||||
|
<|unfolded-io>
|
||||||
(pi-sum 1 3)
|
(pi-sum 1 3)
|
||||||
<|unfolded-io>
|
<|unfolded-io>
|
||||||
0.3333333333333333
|
0.3333333333333333
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
<\unfolded-io|Scheme] >
|
<\unfolded-io>
|
||||||
|
\<gtr\>\
|
||||||
|
<|unfolded-io>
|
||||||
(define (sum term a next b)
|
(define (sum term a next b)
|
||||||
|
|
||||||
\ \ (if (\<gtr\> a b)
|
\ \ (if (\<gtr\> a b)
|
||||||
|
@ -80,13 +96,17 @@
|
||||||
sum
|
sum
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
<\unfolded-io|Scheme] >
|
<\unfolded-io>
|
||||||
|
\<gtr\>\
|
||||||
|
<|unfolded-io>
|
||||||
(define (inc n) (+ n 1))
|
(define (inc n) (+ n 1))
|
||||||
<|unfolded-io>
|
<|unfolded-io>
|
||||||
inc
|
inc
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
<\unfolded-io|Scheme] >
|
<\unfolded-io>
|
||||||
|
\<gtr\>\
|
||||||
|
<|unfolded-io>
|
||||||
(define (sum-cubes-2 a b)
|
(define (sum-cubes-2 a b)
|
||||||
|
|
||||||
\ \ (sum cube a inc b))
|
\ \ (sum cube a inc b))
|
||||||
|
@ -94,23 +114,25 @@
|
||||||
sum-cubes-2
|
sum-cubes-2
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
<\unfolded-io|Scheme] >
|
<\unfolded-io>
|
||||||
|
\<gtr\>\
|
||||||
|
<|unfolded-io>
|
||||||
(sum-cubes-2 1 3)
|
(sum-cubes-2 1 3)
|
||||||
<|unfolded-io>
|
<|unfolded-io>
|
||||||
36
|
36
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
<\input|Scheme] >
|
<\unfolded-io>
|
||||||
\;
|
\<gtr\>\
|
||||||
</input>
|
<|unfolded-io>
|
||||||
|
|
||||||
<\unfolded-io|Scheme] >
|
|
||||||
(define (identity x) x)
|
(define (identity x) x)
|
||||||
<|unfolded-io>
|
<|unfolded-io>
|
||||||
identity
|
identity
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
<\unfolded-io|Scheme] >
|
<\unfolded-io>
|
||||||
|
\<gtr\>\
|
||||||
|
<|unfolded-io>
|
||||||
(define (sum-integers-2 a b)
|
(define (sum-integers-2 a b)
|
||||||
|
|
||||||
\ \ (sum identity a inc b))
|
\ \ (sum identity a inc b))
|
||||||
|
@ -118,13 +140,17 @@
|
||||||
sum-integers-2
|
sum-integers-2
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
<\unfolded-io|Scheme] >
|
<\unfolded-io>
|
||||||
|
\<gtr\>\
|
||||||
|
<|unfolded-io>
|
||||||
(sum-integers-2 1 10)
|
(sum-integers-2 1 10)
|
||||||
<|unfolded-io>
|
<|unfolded-io>
|
||||||
55
|
55
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
<\unfolded-io|Scheme] >
|
<\unfolded-io>
|
||||||
|
\<gtr\>\
|
||||||
|
<|unfolded-io>
|
||||||
(define (pi-sum-2 a b)
|
(define (pi-sum-2 a b)
|
||||||
|
|
||||||
\ \ (define (pi-term x)
|
\ \ (define (pi-term x)
|
||||||
|
@ -140,47 +166,61 @@
|
||||||
pi-sum-2
|
pi-sum-2
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
<\unfolded-io|Scheme] >
|
<\unfolded-io>
|
||||||
|
\<gtr\>\
|
||||||
|
<|unfolded-io>
|
||||||
(* 8 (pi-sum-2 1 1000))
|
(* 8 (pi-sum-2 1 1000))
|
||||||
<|unfolded-io>
|
<|unfolded-io>
|
||||||
3.139592655589783
|
3.139592655589783
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
<\input|Scheme] >
|
<\input>
|
||||||
|
\<gtr\>\
|
||||||
|
<|input>
|
||||||
\;
|
\;
|
||||||
</input>
|
</input>
|
||||||
</session>
|
</session>
|
||||||
|
|
||||||
<subsection*|1.3.2>
|
<subsection*|1.3.2>
|
||||||
|
|
||||||
<\session|scheme|default>
|
<\session|s7|default>
|
||||||
<\unfolded-io|Scheme] >
|
<\unfolded-io>
|
||||||
|
\<gtr\>\
|
||||||
|
<|unfolded-io>
|
||||||
(define (square x) (* x x))
|
(define (square x) (* x x))
|
||||||
<|unfolded-io>
|
<|unfolded-io>
|
||||||
square
|
square
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
<\unfolded-io|Scheme] >
|
<\unfolded-io>
|
||||||
|
\<gtr\>\
|
||||||
|
<|unfolded-io>
|
||||||
((lambda (x y z) (+ x y (square z))) 1 2 3)
|
((lambda (x y z) (+ x y (square z))) 1 2 3)
|
||||||
<|unfolded-io>
|
<|unfolded-io>
|
||||||
12
|
12
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
<\input|Scheme] >
|
<\input>
|
||||||
|
\<gtr\>\
|
||||||
|
<|input>
|
||||||
\;
|
\;
|
||||||
</input>
|
</input>
|
||||||
</session>
|
</session>
|
||||||
|
|
||||||
<subsection*|1.3.3>
|
<subsection*|1.3.3>
|
||||||
|
|
||||||
<\session|scheme|default>
|
<\session|s7|default>
|
||||||
<\unfolded-io|Scheme] >
|
<\unfolded-io>
|
||||||
|
\<gtr\>\
|
||||||
|
<|unfolded-io>
|
||||||
(define (average x y) (/ (+ x y) 2))
|
(define (average x y) (/ (+ x y) 2))
|
||||||
<|unfolded-io>
|
<|unfolded-io>
|
||||||
average
|
average
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
<\unfolded-io|Scheme] >
|
<\unfolded-io>
|
||||||
|
\<gtr\>\
|
||||||
|
<|unfolded-io>
|
||||||
(define (search f neg-point pos-point)
|
(define (search f neg-point pos-point)
|
||||||
|
|
||||||
\ \ (let ((midpoint (average neg-point pos-point)))
|
\ \ (let ((midpoint (average neg-point pos-point)))
|
||||||
|
@ -204,7 +244,9 @@
|
||||||
search
|
search
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
<\unfolded-io|Scheme] >
|
<\unfolded-io>
|
||||||
|
\<gtr\>\
|
||||||
|
<|unfolded-io>
|
||||||
(define (close-enough? x y)
|
(define (close-enough? x y)
|
||||||
|
|
||||||
\ \ (\<less\> (abs (- x y)) 0.001))
|
\ \ (\<less\> (abs (- x y)) 0.001))
|
||||||
|
@ -212,7 +254,9 @@
|
||||||
close-enough?
|
close-enough?
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
<\unfolded-io|Scheme] >
|
<\unfolded-io>
|
||||||
|
\<gtr\>\
|
||||||
|
<|unfolded-io>
|
||||||
(define (half-interval-method f a b)
|
(define (half-interval-method f a b)
|
||||||
|
|
||||||
\ \ (let ((a-value (f a))
|
\ \ (let ((a-value (f a))
|
||||||
|
@ -234,13 +278,17 @@
|
||||||
half-interval-method
|
half-interval-method
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
<\unfolded-io|Scheme] >
|
<\unfolded-io>
|
||||||
|
\<gtr\>\
|
||||||
|
<|unfolded-io>
|
||||||
(half-interval-method sin 2.0 4.0)
|
(half-interval-method sin 2.0 4.0)
|
||||||
<|unfolded-io>
|
<|unfolded-io>
|
||||||
3.14111328125
|
3.14111328125
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
<\unfolded-io|Scheme] >
|
<\unfolded-io>
|
||||||
|
\<gtr\>\
|
||||||
|
<|unfolded-io>
|
||||||
(half-interval-method (lambda (x) (- (* x x x) (* 2 x) 3))
|
(half-interval-method (lambda (x) (- (* x x x) (* 2 x) 3))
|
||||||
|
|
||||||
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 1.0
|
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 1.0
|
||||||
|
@ -250,17 +298,17 @@
|
||||||
1.89306640625
|
1.89306640625
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
<\input|Scheme] >
|
<\unfolded-io>
|
||||||
\;
|
\<gtr\>\
|
||||||
</input>
|
<|unfolded-io>
|
||||||
|
|
||||||
<\unfolded-io|Scheme] >
|
|
||||||
(define tolerance 0.00001)
|
(define tolerance 0.00001)
|
||||||
<|unfolded-io>
|
<|unfolded-io>
|
||||||
0.00001
|
0.00001
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
<\unfolded-io|Scheme] >
|
<\unfolded-io>
|
||||||
|
\<gtr\>\
|
||||||
|
<|unfolded-io>
|
||||||
(define (fixed-point f first-guess)
|
(define (fixed-point f first-guess)
|
||||||
|
|
||||||
\ \ (define (close-enough? v1 v2)
|
\ \ (define (close-enough? v1 v2)
|
||||||
|
@ -282,13 +330,17 @@
|
||||||
fixed-point
|
fixed-point
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
<\unfolded-io|Scheme] >
|
<\unfolded-io>
|
||||||
|
\<gtr\>\
|
||||||
|
<|unfolded-io>
|
||||||
(fixed-point cos 1.0)
|
(fixed-point cos 1.0)
|
||||||
<|unfolded-io>
|
<|unfolded-io>
|
||||||
0.7390822985224024
|
0.7390822985224024
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
<\unfolded-io|Scheme] >
|
<\unfolded-io>
|
||||||
|
\<gtr\>\
|
||||||
|
<|unfolded-io>
|
||||||
(fixed-point (lambda (y) (+ (sin y) (cos y)))
|
(fixed-point (lambda (y) (+ (sin y) (cos y)))
|
||||||
|
|
||||||
\ \ \ \ \ \ \ \ \ \ \ \ \ 1.0)
|
\ \ \ \ \ \ \ \ \ \ \ \ \ 1.0)
|
||||||
|
@ -296,7 +348,9 @@
|
||||||
1.2587315962971173
|
1.2587315962971173
|
||||||
</unfolded-io>
|
</unfolded-io>
|
||||||
|
|
||||||
<\input|Scheme] >
|
<\input>
|
||||||
|
\<gtr\>\
|
||||||
|
<|input>
|
||||||
\;
|
\;
|
||||||
</input>
|
</input>
|
||||||
</session>
|
</session>
|
||||||
|
|
Loading…
Reference in New Issue