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>
|
||||
|
||||
<style|<tuple|generic|no-page-numbers|british|reduced-margins>>
|
||||
<style|<tuple|generic|no-page-numbers|british|reduced-margins|s7>>
|
||||
|
||||
<\body>
|
||||
<section*|Orders of Growth>
|
||||
|
||||
<\session|scheme|default>
|
||||
<\unfolded-io|Scheme] >
|
||||
<\session|s7|default>
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
(define (expt-1 b n)
|
||||
|
||||
\ \ (if (= n 0)
|
||||
|
@ -18,19 +20,25 @@
|
|||
expt-1
|
||||
</unfolded-io>
|
||||
|
||||
<\unfolded-io|Scheme] >
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
(expt-1 1 0)
|
||||
<|unfolded-io>
|
||||
1
|
||||
</unfolded-io>
|
||||
|
||||
<\unfolded-io|Scheme] >
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
(expt-1 2 3)
|
||||
<|unfolded-io>
|
||||
8
|
||||
</unfolded-io>
|
||||
|
||||
<\unfolded-io|Scheme] >
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
(define (expt-2 b n)
|
||||
|
||||
\ \ (define (expt-iter b counter product)
|
||||
|
@ -52,21 +60,27 @@
|
|||
expt-2
|
||||
</unfolded-io>
|
||||
|
||||
<\unfolded-io|Scheme] >
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
(expt-2 3 3)
|
||||
<|unfolded-io>
|
||||
27
|
||||
</unfolded-io>
|
||||
|
||||
<\input|Scheme] >
|
||||
<\input>
|
||||
\<gtr\>\
|
||||
<|input>
|
||||
\;
|
||||
</input>
|
||||
</session>
|
||||
|
||||
<section*|Exponentiation>
|
||||
|
||||
<\session|scheme|default>
|
||||
<\unfolded-io|Scheme] >
|
||||
<\session|s7|default>
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
(define (fast-expt b n)
|
||||
|
||||
\ \ (define (even? n)
|
||||
|
@ -86,17 +100,27 @@
|
|||
fast-expt
|
||||
</unfolded-io>
|
||||
|
||||
<\unfolded-io|Scheme] >
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
(fast-expt 3 3)
|
||||
<|unfolded-io>
|
||||
27
|
||||
</unfolded-io>
|
||||
|
||||
<\input>
|
||||
\<gtr\>\
|
||||
<|input>
|
||||
\;
|
||||
</input>
|
||||
</session>
|
||||
|
||||
<section*|Greatest Common Divisors>
|
||||
|
||||
<\session|scheme|default>
|
||||
<\unfolded-io|Scheme] >
|
||||
<\session|s7|default>
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
(define (gcd a b)
|
||||
|
||||
\ \ (if (= b 0)
|
||||
|
@ -108,11 +132,19 @@
|
|||
gcd
|
||||
</unfolded-io>
|
||||
|
||||
<\unfolded-io|Scheme] >
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
(gcd 10 20)
|
||||
<|unfolded-io>
|
||||
10
|
||||
</unfolded-io>
|
||||
|
||||
<\input>
|
||||
\<gtr\>\
|
||||
<|input>
|
||||
\;
|
||||
</input>
|
||||
</session>
|
||||
</body>
|
||||
|
||||
|
@ -128,4 +160,22 @@
|
|||
<associate|auto-2|<tuple|?|?>>
|
||||
<associate|auto-3|<tuple|?|?>>
|
||||
</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>
|
||||
|
||||
<style|<tuple|generic|no-page-numbers|chinese>>
|
||||
<style|<tuple|generic|no-page-numbers|chinese|s7>>
|
||||
|
||||
<\body>
|
||||
<subsection*|1.3.1>
|
||||
|
||||
<\session|scheme|default>
|
||||
<\unfolded-io|Scheme] >
|
||||
<\session|s7|default>
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
(define (cube x) (* x x x))
|
||||
<|unfolded-io>
|
||||
cube
|
||||
</unfolded-io>
|
||||
|
||||
<\unfolded-io|Scheme] >
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
(define (sum-integers a b)
|
||||
|
||||
\ \ (if (\<gtr\> a b)
|
||||
|
@ -24,7 +28,9 @@
|
|||
sum-integers
|
||||
</unfolded-io>
|
||||
|
||||
<\unfolded-io|Scheme] >
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
(define (sum-cubes a b)
|
||||
|
||||
\ \ (if (\<gtr\> a b)
|
||||
|
@ -36,7 +42,9 @@
|
|||
sum-cubes
|
||||
</unfolded-io>
|
||||
|
||||
<\unfolded-io|Scheme] >
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
(define (pi-sum a b)
|
||||
|
||||
\ \ (if (\<gtr\> a b)
|
||||
|
@ -48,25 +56,33 @@
|
|||
pi-sum
|
||||
</unfolded-io>
|
||||
|
||||
<\unfolded-io|Scheme] >
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
(sum-integers 1 3)
|
||||
<|unfolded-io>
|
||||
6
|
||||
</unfolded-io>
|
||||
|
||||
<\unfolded-io|Scheme] >
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
(sum-cubes 1 3)
|
||||
<|unfolded-io>
|
||||
36
|
||||
</unfolded-io>
|
||||
|
||||
<\unfolded-io|Scheme] >
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
(pi-sum 1 3)
|
||||
<|unfolded-io>
|
||||
0.3333333333333333
|
||||
</unfolded-io>
|
||||
|
||||
<\unfolded-io|Scheme] >
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
(define (sum term a next b)
|
||||
|
||||
\ \ (if (\<gtr\> a b)
|
||||
|
@ -80,13 +96,17 @@
|
|||
sum
|
||||
</unfolded-io>
|
||||
|
||||
<\unfolded-io|Scheme] >
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
(define (inc n) (+ n 1))
|
||||
<|unfolded-io>
|
||||
inc
|
||||
</unfolded-io>
|
||||
|
||||
<\unfolded-io|Scheme] >
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
(define (sum-cubes-2 a b)
|
||||
|
||||
\ \ (sum cube a inc b))
|
||||
|
@ -94,23 +114,25 @@
|
|||
sum-cubes-2
|
||||
</unfolded-io>
|
||||
|
||||
<\unfolded-io|Scheme] >
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
(sum-cubes-2 1 3)
|
||||
<|unfolded-io>
|
||||
36
|
||||
</unfolded-io>
|
||||
|
||||
<\input|Scheme] >
|
||||
\;
|
||||
</input>
|
||||
|
||||
<\unfolded-io|Scheme] >
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
(define (identity x) x)
|
||||
<|unfolded-io>
|
||||
identity
|
||||
</unfolded-io>
|
||||
|
||||
<\unfolded-io|Scheme] >
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
(define (sum-integers-2 a b)
|
||||
|
||||
\ \ (sum identity a inc b))
|
||||
|
@ -118,13 +140,17 @@
|
|||
sum-integers-2
|
||||
</unfolded-io>
|
||||
|
||||
<\unfolded-io|Scheme] >
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
(sum-integers-2 1 10)
|
||||
<|unfolded-io>
|
||||
55
|
||||
</unfolded-io>
|
||||
|
||||
<\unfolded-io|Scheme] >
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
(define (pi-sum-2 a b)
|
||||
|
||||
\ \ (define (pi-term x)
|
||||
|
@ -140,47 +166,61 @@
|
|||
pi-sum-2
|
||||
</unfolded-io>
|
||||
|
||||
<\unfolded-io|Scheme] >
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
(* 8 (pi-sum-2 1 1000))
|
||||
<|unfolded-io>
|
||||
3.139592655589783
|
||||
</unfolded-io>
|
||||
|
||||
<\input|Scheme] >
|
||||
<\input>
|
||||
\<gtr\>\
|
||||
<|input>
|
||||
\;
|
||||
</input>
|
||||
</session>
|
||||
|
||||
<subsection*|1.3.2>
|
||||
|
||||
<\session|scheme|default>
|
||||
<\unfolded-io|Scheme] >
|
||||
<\session|s7|default>
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
(define (square x) (* x x))
|
||||
<|unfolded-io>
|
||||
square
|
||||
</unfolded-io>
|
||||
|
||||
<\unfolded-io|Scheme] >
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
((lambda (x y z) (+ x y (square z))) 1 2 3)
|
||||
<|unfolded-io>
|
||||
12
|
||||
</unfolded-io>
|
||||
|
||||
<\input|Scheme] >
|
||||
<\input>
|
||||
\<gtr\>\
|
||||
<|input>
|
||||
\;
|
||||
</input>
|
||||
</session>
|
||||
|
||||
<subsection*|1.3.3>
|
||||
|
||||
<\session|scheme|default>
|
||||
<\unfolded-io|Scheme] >
|
||||
<\session|s7|default>
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
(define (average x y) (/ (+ x y) 2))
|
||||
<|unfolded-io>
|
||||
average
|
||||
</unfolded-io>
|
||||
|
||||
<\unfolded-io|Scheme] >
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
(define (search f neg-point pos-point)
|
||||
|
||||
\ \ (let ((midpoint (average neg-point pos-point)))
|
||||
|
@ -204,7 +244,9 @@
|
|||
search
|
||||
</unfolded-io>
|
||||
|
||||
<\unfolded-io|Scheme] >
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
(define (close-enough? x y)
|
||||
|
||||
\ \ (\<less\> (abs (- x y)) 0.001))
|
||||
|
@ -212,7 +254,9 @@
|
|||
close-enough?
|
||||
</unfolded-io>
|
||||
|
||||
<\unfolded-io|Scheme] >
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
(define (half-interval-method f a b)
|
||||
|
||||
\ \ (let ((a-value (f a))
|
||||
|
@ -234,13 +278,17 @@
|
|||
half-interval-method
|
||||
</unfolded-io>
|
||||
|
||||
<\unfolded-io|Scheme] >
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
(half-interval-method sin 2.0 4.0)
|
||||
<|unfolded-io>
|
||||
3.14111328125
|
||||
</unfolded-io>
|
||||
|
||||
<\unfolded-io|Scheme] >
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
(half-interval-method (lambda (x) (- (* x x x) (* 2 x) 3))
|
||||
|
||||
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 1.0
|
||||
|
@ -250,17 +298,17 @@
|
|||
1.89306640625
|
||||
</unfolded-io>
|
||||
|
||||
<\input|Scheme] >
|
||||
\;
|
||||
</input>
|
||||
|
||||
<\unfolded-io|Scheme] >
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
(define tolerance 0.00001)
|
||||
<|unfolded-io>
|
||||
0.00001
|
||||
</unfolded-io>
|
||||
|
||||
<\unfolded-io|Scheme] >
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
(define (fixed-point f first-guess)
|
||||
|
||||
\ \ (define (close-enough? v1 v2)
|
||||
|
@ -282,13 +330,17 @@
|
|||
fixed-point
|
||||
</unfolded-io>
|
||||
|
||||
<\unfolded-io|Scheme] >
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
(fixed-point cos 1.0)
|
||||
<|unfolded-io>
|
||||
0.7390822985224024
|
||||
</unfolded-io>
|
||||
|
||||
<\unfolded-io|Scheme] >
|
||||
<\unfolded-io>
|
||||
\<gtr\>\
|
||||
<|unfolded-io>
|
||||
(fixed-point (lambda (y) (+ (sin y) (cos y)))
|
||||
|
||||
\ \ \ \ \ \ \ \ \ \ \ \ \ 1.0)
|
||||
|
@ -296,7 +348,9 @@
|
|||
1.2587315962971173
|
||||
</unfolded-io>
|
||||
|
||||
<\input|Scheme] >
|
||||
<\input>
|
||||
\<gtr\>\
|
||||
<|input>
|
||||
\;
|
||||
</input>
|
||||
</session>
|
||||
|
|
Loading…
Reference in New Issue