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
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
3
3
4
4
4
4
5
5
6
7
7
8
9
10
10
11
i
ii
SICP Answers Documentation, Release present
SICP Scheme Gambit-C Guile.
Gambit-C , code
$ gsi 1.2.scm
:
$ gsc -exe 1.2.scm
$ ./1.2
Note: Scheme R5 RS , sqrt, even? , , .
,.
Contents:
Contents
1
SICP Answers Documentation, Release present
2
Contents
CHAPTER
1
Chapter 1
1.1
> 10
10
> (+ 5 3 4)
12
> (- 9 1)
8
> (/ 6 2)
3
> (define a 3)
> (define b (+ a 1))
> (+ a b (* a b))
19
> (= a b)
#f
> (if (and (> b a) (< b (* a b)))
b
a)
4
> (cond ((= a 4) 6)
((= b 4) (+ 6 7 a))
(else 25))
16
> (+ 2 (if (> b a) b a))
6
> (* (cond ((> a b) a)
((< a b) b)
(else -1))
(+ a 1))
16
3
SICP Answers Documentation, Release present
1.2
(define exp (/ (+ 5 4 (- 2
(- 3
(+ 6
(/ 4 5)))))
(* 3 (- 6 2) (- 2 7))))
(display exp)
(newline)
1.3
;; Define a procedure that takes three numbers as arguments and returns the sum of
˓→the squares of the two larger numbers.
(define (<= a b)
(not (> a b)))
(define
(cond
((and
((and
(else
(bigger-2-sum-of-square a b c)
(<= a b) (<= a c)) (+ (* b b) (* c c)))
(<= b a) (<= b c)) (+ (* a a) (* c c)))
(+ (* a a) (* b b)))))
(display (bigger-2-sum-of-square 1 2 5))
;;(display (bigger-2-sum-of-square 1 1 5))
;;(display (bigger-2-sum-of-square 1 1 1))
1.4
(define (a-plus-abs-b a b)
;; b > 0, (if (> b 0) + -)
;; , .
;; Haskell (), .
((if (> b 0) + -) a b))
+;
-.
(display (a-plus-abs-b 5 -4))
1.5
(define (p) (p))
(define (test x y)
(if (= x 0)
0
y))
;; (test 0 (p))
4
Chapter 1. Chapter 1
SICP Answers Documentation, Release present
;; , ( (p) ); (p) , ;
;; (if ...) , x=0, 0.
1.6
(define (good-enough? guess x)
(< (abs (- (* guess guess) x)) 0.001))
(define (average a b)
(/ (+ a b) 2))
(define (improve-guess guess x)
(average guess (/ x guess)))
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve-guess guess x) x)))
;(define (sqrt-iter guess x)
; (cond ((good-enough? guess x) guess)
; (else (sqrt-iter (improve-guess guess x) x))))
(define (sqrt? x)
(sqrt-iter 1.0 x))
;(display (sqrt? 9)) ; 3.00009155413138
; ----------------------------------------------(define (new-if predicate then-clause else-clause)
(cond (predicate then-clause)
(else else-clause)))
; (), else-clause ,
;
sqrt-iter sqrt-iter , ,
; , .
1.7
(load "1.6.scm")
;(display
;(display
;(display
;(display
;(display
;(display
;(display
;(display
(sqrt?
(sqrt?
(sqrt?
(sqrt?
(sqrt?
(sqrt?
(sqrt?
(sqrt?
9)); 3.00009155413138
900000000000)); 948683.2980505137
9000000000000)); 3000000.
0.4)); .6324638844301766
0.000000004)); .03125004262498841
0.00000000000000004)); .03125000000000043
0.000000000000000004)); .03125000000000004
0.0000000000000000004)); .03125
; , ( good-enough? , ,
1.6. 1.6
5
SICP Answers Documentation, Release present
; , good-enough? );
;
(X0 - X1)/X0 .
(define (improve-guess guess x)
(average guess (/ x guess)))
(define (good-enough? guess x)
(< (/ (abs (- guess (improve-guess guess x))) guess) 0.001))
(define (average a b)
(/ (+ a b) 2))
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve-guess guess x) x)))
(define (sqrt? x)
(sqrt-iter 1.0 x))
;(display (sqrt? 900000000000000000000000000000000000000000))
;(display (sqrt? 0.000000000000000000000000000000000000000004))
; (, ).
(define (good-enough? guess x)
(< (/ (abs (- (square guess) x)) guess) (* guess 0.0001)))
1.8
;
(X0 - X1)/X0 .
(define (improve-guess guess x)
(/ (+ (/ x (* guess guess)) (* 2 guess)) 3))
(define (good-enough? guess x)
(< (/ (abs (- guess (improve-guess guess x))) guess) 0.001))
;(< (abs (- (* guess guess guess) x)) 0.001))
(define (cube-root-iter guess x)
(if (good-enough? guess x)
guess
(cube-root-iter (improve-guess guess x) x)))
(define (cube-root x)
(cube-root-iter 1.0 x))
(display (cube-root 100000000000000000000000000000000)); 4.641721617671784e10
6
Chapter 1. Chapter 1
SICP Answers Documentation, Release present
1.9
(+ 4
(inc
(inc
(inc
(inc
(inc
(inc
(inc
(inc
(inc
(inc
(inc
(inc
9
5)
(+ (dec 4) 5))
(+ 3 5))
(inc (+ (dec 3) 5)))
(inc (+ 2 5)))
(inc (inc (+ (dec 2) 5))))
(inc (inc (+ 1 5))))
(inc (inc (inc (+ (dec 1) 5)))))
(inc (inc (inc (+ 0 5)))))
(inc (inc (inc 5))))
(inc (inc 6)))
(inc 7))
8)
; `+`
(+
(+
(+
(+
(+
(+
(+
(+
(+
9
`inc` ,
4 5)
(dec
3 6)
(dec
2 7)
(dec
1 8)
(dec
0 9)
4) (inc 5))
3) (inc 6))
2) (inc 7))
1) (inc 8))
; `+` ,
1.10
(A
(A
(A
(A
(A
(A
(A
(A
(A
(A
(A
(A
(A
(A
(A
(A
(A
(A
(A
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
10)
(A 1
(A 0
(A 0
(A 0
(A 0
(A 0
(A 0
(A 0
(A 0
(A 0
(A 0
(A 0
(A 0
(A 0
(A 0
(A 0
(A 0
512)
9))
(A 1 8)))
(A 0 (A 1 7))))
(A 0 (A 0 (A 1 6)))))
(A 0 (A 0 (A 0 (A 1 5))))))
(A 0 (A 0 (A 0 (A 0 (A 1 4)))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 1 3))))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 1 2)))))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 1 1))))))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 2)))))))))
(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 4))))))))
(A 0 (A 0 (A 0 (A 0 (A 0 8)))))))
(A 0 (A 0 (A 0 (A 0 16))))))
(A 0 (A 0 (A 0 32)))))
(A 0 (A 0 64))))
(A 0 128)))
256))
1024
1.9. 1.9
7
SICP Answers Documentation, Release present
(A
(A
(A
(A
(A
(A
(A
(A
(A
(A
(A
(A
(A
(A
(A
2
1
1
1
1
1
1
1
1
1
1
1
1
1
1
4)
(A 2
(A 1
(A 1
(A 1
(A 1
(A 1
(A 1
(A 0
(A 0
(A 0
(A 0
(A 0
(A 0
16)
3))
(A 2
(A 1
(A 1
(A 0
(A 0
4))
(A 1
(A 0
(A 0
(A 0
(A 0
8))
2)))
(A 2 1))))
2)))
(A 1 1))))
2)))
3)))
(A 1 2))))
(A 0 (A 1 1)))))
(A 0 2))))
4)))
(A 1 10) : 2^{16}.
(A
(A
(A
(A
3
2
2
2
3)
(A 3 2))
(A 2 (A 3 1)))
(A 2 2))
(A 2 2)
4, :
(A 2 (A 2 2))
(A 2 4)
2^{16}
(define (f n) (A 0 n))
(define (f n) (* 2 n))
(f n)
2n.
(define (g n) (A 1 n)
(g n)
2^{n}.
(define (h n) (A 2 n))
: (A 2 n) -> 2^{2^{n}}
(h n)
2^{2^{n}}.
1.11
;
(define (f n)
(if (< n 3)
n
(+ (f (- n 1)) (* 2 (f (- n 2)))
8
Chapter 1. Chapter 1
SICP Answers Documentation, Release present
(* 3 (f (- n 3))))))
;
;
;
;
;
;
;
;
;
: f(0) = 0, f(1) = 1, f(2) = 2
a = f(n-4), b = f(n-3), c = f(n-2)
f(n-1) = 3f(n-4) + 2f(n-3) + f(n-2) = 3a + 2b + c
f(n)
= 3f(n-3) + 2f(n-2) + f(n-1) = 3b + 2a + (3a + 2b + c)
: a -> b, b -> c, c -> (3a + 2b + c), counter = counter -1
: counter = 0, f(n) = a
f(n) , f(n+1), f(n+2) , 2 .
(define (f n)
(define (f-iter a b c counter)
(if (= counter 0)
a
(f-iter b c (+ (* 3 a) (* 2 b) c) (- counter 1))))
(f-iter 0 1 2 n))
; (display (f 4))
;
;
;
;
;
;
;
;
: f(0) = 0, f(1) = 1, f(2) = 2
a = f(n-2), b = f(n-3), c = f(n-4)
f(n-1) = 3f(n-4) + 2f(n-3) + f(n-2) = 3c + 2b + a
f(n)
= 3f(n-3) + 2f(n-2) + f(n-1) = 3b + 2a + (3c + 2b + a)
: a -> (3c + 2b + a), b -> a, c -> b, counter = counter -1
: counter < 3, f(n) = a
.
(define (f n)
(define (f-iter a b c counter)
(if (< counter 3)
a
(f-iter (+ (* 3 c) (* 2 b) a) a b (- counter 1))))
(if (< n 3)
n
(f-iter 2 1 0 n)))
(display (f 4))
1.12
.
(define (pascal row col)
(cond
; 0
((or
(< row 1)
(< col 1)
(< row col)) 0)
; 1
((or
1.12. 1.12
9
SICP Answers Documentation, Release present
(= col 1)
(= row col)) 1)
;
(else (+ (pascal (- row 1) (- col 1))
(pascal (- row 1) col)))
))
(display (pascal 4 3))
1.16
;
b,
a,
n
; , n :
; ab^(n) = a(b^2)^(n/2)
; a -> a, b -> b^2, n -> (n/2)
; n :
; ab^(n) = ab(b^(n-1))
; a -> ab, b -> b, n -> (n-1)
(define (expt b n)
(define (expt-iter a b n)
(cond ((= n 0) a)
((even? n) (expt-iter a (* b b) (/ n 2)))
(else (expt-iter (* a b) b (- n 1)))))
(expt-iter 1 b n))
(display (expt 2 1000))
1.17
(load "common.scm")
;
; b , ab = (2a)(b/2), a -> 2a, b
; b , ab = a(b-1) + a, a -> a, b
(define (multiplied a b)
(define (multiplied-iter a b)
(cond ((= b 0) 0)
((even? b) (multiplied
(else (+ a (multiplied
)
)
(multiplied-iter a b)
)
;
|
;
|
10
-> b/2
-> (b-1)
(double a) (halve b)))
a (- b 1))))
Chapter 1. Chapter 1
SICP Answers Documentation, Release present
;
;
|
\|/
(define (multiplied a b)
(cond ((= b 0) 0)
((even? b) (multiplied (double a) (halve b)))
(else (+ a (multiplied a (- b 1))))
)
)
(display (multiplied 1 3))
(newline)
(display (multiplied 2 100000))
1.18
(load "common.scm")
;
a,
c,
b
; c + ab = c + 2a(b/2)
; c + ab = (c+a) + a(b-1)
(define (fast-multiplied a b)
(define (mutiplied-iter c a b)
(cond ((= b 0) c)
((even? b) (mutiplied-iter c (double a) (halve b)))
(else (mutiplied-iter (+ c a) a (- b 1)))
)
)
(mutiplied-iter 0 a b)
)
(display (fast-multiplied 1 3))
(newline)
(display (fast-multiplied 2 10000000))
1.15. 1.18
11
© Copyright 2026 Paperzz