1. 程式人生 > 實用技巧 >SICP Chapter 1 Exercises

SICP Chapter 1 Exercises

SICP exercise-1.1

Below is a sequence of expressions. What is the result printed by the interpreter in response to each expression? Assume that the sequence is to be evaluated in the order in which it is presented.

10
# 10

(+ 5 3 4)
# 12

(- 9 1)
# 8

(/ 6 2)
# 3

(+ (* 2 4) (- 4 6))
# -16

(define a 3)
# value: a

(define b (+ a 1))
# value: b

(+ 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

SICP exercise-1.2

Translate the following expression into prefix form:

![image-20200908145230558](/Users/ssaylo/Library/Application Support/typora-user-images/image-20200908145230558.png)

my answer (retract problem)

(/ (+ 5
      4 (- 2 (- 3 (+ 6 (/ 4 5))))) 
   (* 3
      (- 6 2)
      (- 2 7)))

SICP exercise-1.3

Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers.

 (define (square x) (* x x)) 
  
 (define (sumsquares x y) (+ (square x) (square y))) 
  
 (define (sqsumlargest a b c) 
     (cond  
         ((and (>= a c) (>= b c)) (sumsquares a b)) 
         ((and (>= b a) (>= c a)) (sumsquares b c)) 
         ((and (>= a b) (>= c b)) (sumsquares a c)))) 

SICP exercise-1.4

Observe that our model of evaluation allows for combinations whose operators are compound expressions. Use this observation to describe the behavior of the following procedure:

(define (a-plus-abs-b a b)
  ((if (> b 0) + -) a b))
 (a-plus-abs-b 1 -3) 
 ((if (> -3 0) + -) 1 -3) 
 ((if #f + -) 1 -3) 
 (- 1 -3) 
 4 
  
 (a-plus-abs-b 1 3) 
 ((if (> 3 0) + -) 1 3) 
 ((if #t + -) 1 3) 
 (+ 1 3) 
 4 

SICP exercise-1.5

Ben Bitdiddle has invented a test to determine whether the interpreter he is faced with is using applicative-order evaluation or normal-order evaluation. He defines the following two procedures:

(define (p) (p))

(define (test x y) 
  (if (= x 0) 
      0 
      y))

Then he evaluates the expression

(test 0 (p))

What behavior will Ben observe with an interpreter that uses applicative-order evaluation? What behavior will he observe with an interpreter that uses normal-order evaluation? Explain your answer. (Assume that the evaluation rule for the special form if is the same whether the interpreter is using normal or applicative order: The predicate expression is evaluated first, and the result determines whether to evaluate the consequent or the alternative expression.)

applicative-order

Using applicative-order evaluation, the evaluation of (test 0 (p)) will never terminates, because (p) is infinitely expanded to itself:

(test 0 (p))
(test 0 (p))
(test 0 (p))

normal-order

Using normal-order evaluation, the expression evaluates, step by step, to 0:

(test 0 (p))
(if (= 0 0) 0 (p))
(if #t 0 (p))
0

SICP exercise-1.6

Alyssa P. Hacker doesn’t see why if needs to be provided as a special form. “Why can’t I just define it as an ordinary procedure in terms of cond?” she asks. Alyssa’s friend Eva Lu Ator claims this can indeed be done, and she defines a new version of if:

(define (new-if predicate 
                then-clause 
                else-clause)
  (cond (predicate then-clause)
        (else else-clause)))

Eva demonstrates the program for Alyssa:

(new-if (= 2 3) 0 5)
5
(new-if (= 1 1) 0 5)
0

Delighted, Alyssa uses new-if to rewrite the square-root program:

(define (sqrt-iter guess x)
  (new-if (good-enough? guess x)
          guess
          (sqrt-iter (improve guess x) x)))

What happens when Alyssa attempts to use this to compute square roots? Explain.

new if 沒有用 normal-order evaluation,而是用了 applicative evaluation...

SICP exercise-1.7

The good-enough? test used in computing square roots will not be very effective for finding the square roots of very small numbers. Also, in real computers, arithmetic operations are almost always performed with limited precision. This makes our test inadequate for very large numbers. Explain these statements, with examples showing how the test fails for small and large numbers. An alternative strategy for implementing good-enough? is to watch how guess changes from one iteration to the next and to stop when the change is a very small fraction of the guess. Design a square-root procedure that uses this kind of end test. Does this work better for small and large numbers?

 (define (sqrt-iter guess oldguess x) 
   (if (good-enough? guess oldguess) 
       guess 
       (sqrt-iter (improve guess x) guess 
                  x))) 
  
  
 (define (good-enough? guess oldguess) 
   (< (abs (- guess oldguess)) 
      (* guess 0.001))) 
  
 (define (sqrt x) 
   (sqrt-iter 1.0 2.0 x)) 

SICP exercise-1.8

Newton’s method for cube roots is based on the fact that if yy is an approximation to the cube root of xx, then a better approximation is given by the value

x/y2+2y3.x/y2+2y3.

Use this formula to implement a cube-root procedure analogous to the square-root procedure. (In 1.3.4 we will see how to implement Newton’s method in general as an abstraction of these square-root and cube-root procedures

 (define (cube-root-iter guess x) 
              (if (good-enough? guess x) 
                   guess 
                   (cube-root-iter (improve guess x) x))) 
  
 (define (improve guess x) 
               (average (/ x (square guess)) (* 2 guess))) 
  
 (define (average x y) 
              (/ (+ x y) 3)) 
                  
 (define (square x) (* x x)) 
  
 (define (good-enough? guess x) 
              (< (abs (- (cube guess) x)) (* guess 0.001))) 
  
 (define (cube x) (* x x x)) 
  
 (define (cube-root x)  
              (if (< x 0)  
                   (* -1 (cube-root-iter 1.0 (abs x)))  
                   (cube-root-iter 1.0 x))) 
  
 (cube-root 27) 
 3.0000005410641766 
  
 (cube-root -27) 
 -3.0000005410641766