SICP Chapter1 Exercise

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
(+ 5 3 4)
(- 9 1)
(/ 6 2)
(+ (* 2 4) (- 4 6))
(define a 3)
(define b (+ a 1))
(+ a b (* a b))
(= a b)
(if (and (> b a) (< b (* a b)))
b
    a)
(cond ((= a 4) 6)
      ((= b 4) (+ 6 7 a))
      (else 25))
(+ 2 (if (> b a) b a))
(* (cond ((> a b) a)
         ((< a b) b)
         (else -1))
   (+ a 1))

:

10
12
8
3
6
(= a 3)
(= b 4)
19
#f
4
16
6
16

Exercise 1.2.

Translate the following expression into prefix form
5+4+(2(3(6+45)))3(62)(27) 5 + 4 + ( 2 − ( 3 − ( 6 + 4 5 ) ) ) 3 ( 6 − 2 ) ( 2 − 7 )
:

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

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 (sum-of-squares x y)
  (+ (square x ) (square y)))
(define (smaller x y)
  (if (< x y)
      x
      y))
(define (bigger x y)
  (if (> x y)
      x
      y))
(define (sum-of-squares-larger-two x y z)
  (sum-of-squares (bigger x y)
                  (bigger (smaller x y) z)))

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))

calculate the absolute value of b b , then add a with it

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.)
:
An interpreter that uses applicative-order evaluation will in the loop forever
An interpreter that uses normal-order evaluation will return 0

Applicative-order evaluation first evaluates the operator and operands and then applies the resulting procedure to the resulting arguments
Normal-order evaluation would not evaluate the operands until their values were needed

Exercise 1.6.

Alyssa P. Hacker doesn’t see whyifneeds 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: 32

(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.
:
The produce will in the loop forever because newif n e w − i f will evaluate both statements, but if i f will only evaluate one of them

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 rea\
l computers, arithmetic operations are almost always performed with limited pre\
cision. This makes our test inadequate for very large numbers. Explain these st\
atements, with examples showing how the test fails for small and large numbers.\
An alternative strategy for implementing good-enough? is to watch how guess ch\
anges from one iteration to the next and to stop when the change is a very smal\
l 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 (abs x)
  (if (< x 0)
      (- x)
      x))
(define (sqrt c)
  (sqrt-iter 0.0 1.0 c))
(define (sqrt-iter x2 x1 c)
  (if (< (abs (- x2 x1)) 0.000001)
      x1
      (sqrt-iter x1
                 (/ (+ x1 (/ c x1)) 2.0)
                 c)))

Exercise 1.8.

Newton’s method for cube roots is based on the fact that if y is an approximation to the cube root of x, then a better approximation is given by the value
xy2+2y3 x y 2 + 2 y 3
Use this formula to implement a cube-root procedure analogous to the square-root procedure. (In section 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-roots c)
  (cube-roots-iter 0.0 1.0 c))
(define (cube-roots-iter x2 x1 c)
  (if (< (abs (- x2 x1)) 0.000001)
      x1
      (cube-roots-iter x1
               (/ (+ (/ c x1 x1) (* 2.0 x1)) 3.0)
               c)))

1.1.7 Example: Square Roots by Newton’s Method

(define (sqrt-iter guess x)
  (if (good-enough? guess x)
      guess
      (sqrt-iter (improve guess x)
                 x)))
(define (improve guess x)
  (average guess (/ x guess)))
(define (average x y)
  (/ (+ x y) 2))
(define (sqrt x)
  (sqrt-iter 1.0 x))
(define (good-enough? guess x)
  (< (abs (- (square guess) x)) 0.001))
(define (abs x)
  (if (> x 0)
      x
      (- x)))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值