DEV Community

Cover image for SICP Exercises Chapter 1 Part 1
Ata Kuyumcu
Ata Kuyumcu

Posted on • Updated on

SICP Exercises Chapter 1 Part 1

I started reading through "Structure and Interpretation of Computer Programs" and thought it would be a good idea to publish my progress here. Here are my solutions for the first ten exercises in the book.

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.

Answer:

10
; 10

(+ 5 3 4)
; 12

(- 9 1)
; 8

(/ 6 2)
; 3

(+ (* 2 4) (- 4 6))
; 6

(define a 3)
; 3

(define b (+ a 1))
; 4

(+ a b (* a b))
; 19

(= a b)
; false

(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
Enter fullscreen mode Exit fullscreen mode

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\over5})))\over3(6 - 2)(2 - 7)}

Answer:

(/ (+ 5 4 (- 2 3 (/ 4 5)))
   (* 3 (- 6 2) (- 2 7)))
Enter fullscreen mode Exit fullscreen mode

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

Answer:

(define (sqr a) (* a a))

(define (caterpillar x y z)
  (- (+ (sqr x) (sqr y) (sqr z))
     (sqr (min x y z))))

(define (bookworm x y z)
  (if (> x y)
      (+ (sqr x) (sqr (max y z)))
      (+ (sqr y) (sqr (max x z)))))

(define (ladybug x y z)
  (define sumsqr a b (+ (sqr a) (sqr b)))
  (max (sumsqr x y) (sumsqr x z) (sumsqr y z)))
Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode

Answer: If b is positive, add it to a. Otherwise, subtract it from a. This gives us the sum of a and the absolute value of b.

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))
Enter fullscreen mode Exit fullscreen mode

Then he evaluates the expression

(test 0 (p))
Enter fullscreen mode Exit fullscreen mode

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

Answer: An interpreter that uses applicative-order evaluation would evaluate the parameter (p) in the expression (test 0 (p)) before it is expanded. Trying to evaluate the expression (p) would cause an infinite loop.

In normal-order evaluation, the expression (test 0 (p)) is expanded to (if (= 0 0) 0 (p)) in which (p) isn't evaluated because the predicate (= 0 0) evaluates to true.

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)))
Enter fullscreen mode Exit fullscreen mode

Eva demonstrates the program for Alyssa:

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

(new-if (= 1 1) 0 5)
; 0
Enter fullscreen mode Exit fullscreen mode

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)))
Enter fullscreen mode Exit fullscreen mode

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

Answer: The recursion stop condition does not work as intended as new-if evaluates else-clause even when the predicate evaluates to false.

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?

Answer: The given test good-enough? will not be very effective for finding the square roots of very small numbers because of the fixed tolerance value 0.001, e.g. (sqrt 0.000004) would return 0.03129261341049664. Also, most computers have native support for precision up to 64 bits but variable-precision can be achieved in software with libraries such as GMP.

(define (square x) (* x x))
(define (average x y) (/ (+ x y) 2))
(define (improve guess x) (average guess (/ x guess)))
(define (good-enough? guess x) (< (abs (- (square guess) x)) 0.001))

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

(define (sqrt x) (sqrt-iter 1.0 x))

(sqrt 0.000004)
; 0.03129261341049664 :(

(sqrt 9999999999999)
; 3162277.660168221 :)

(define (good-enough? previous-guess guess x)
  (< (/ (abs (- guess previous-guess)) x) 0.001))

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

(define (sqrt x) (sqrt-iter 0 1.0 x))

(sqrt 0.000004)
; 0.0020000000000000235 :)

(sqrt 9999999999999)
; 1.0
Enter fullscreen mode Exit fullscreen mode

As you can see above, our solution works better for small numbers but not so much for big numbers.

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

x/y2+2y3 x / y^2 + 2y \over 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.)

Answer:

(define (improve-cube guess x)
  (/ (+ (/ x guess guess) (* 2 guess)) 3))

(define (cbrt-iter previous-guess guess x)
  (if (good-enough? previous-guess guess x)
      guess
      (cbrt-iter guess (improve-cube guess x) x)))
Enter fullscreen mode Exit fullscreen mode

Exercise 1.9: Each of the following two procedures defines a method for adding two positive integers in terms of the procedures inc, which increments its argument by 1, and dec, which decrements its argument by 1.

(define (+ a b)
  (if (= a 0) b (inc (+ (dec a) b))))

(define (+ a b)
  (if (= a 0) b (+ (dec a) (inc b))))
Enter fullscreen mode Exit fullscreen mode

Using the substitution model, illustrate the process generated by each procedure in evaluating (+ 4 5). Are these processes iterative or recursive?

Answer:

(+ 4 5)
(inc (+ (dec 4) 5))
(inc (inc (+ (dec 3) 5)))
(inc (inc (inc (+ (dec 2) 5))))
(inc (inc (inc (inc (+ (dec 1) 5)))))
(inc (inc (inc (inc 5))))
(inc (inc (inc 6)))
(inc (inc 7))
(inc 8)
9
Enter fullscreen mode Exit fullscreen mode

This process is clearly recursive, it grows with the recursion and rolls back when it hits the stop condition.

(+ 4 5)
(+ 3 6)
(+ 2 7)
(+ 1 8)
(+ 0 9)
9
Enter fullscreen mode Exit fullscreen mode

This process is iterative thanks to applicative order evaluation.

Exercise 1.10: The following procedure computes a mathematical function called Ackermann’s function.

(define (A x y)
  (cond ((= y 0) 0)
        ((= x 0) (* 2 y))
        ((= y 1) 2)
        (else (A (- x 1) (A x (- y 1))))))
Enter fullscreen mode Exit fullscreen mode

What are the values of the following expressions?

(A 1 10)
(A 2 4)
(A 3 3)
Enter fullscreen mode Exit fullscreen mode

Consider the following procedures, where A is the procedure defined above:

(define (f n) (A 0 n))
(define (g n) (A 1 n))
(define (h n) (A 2 n))
(define (k n) (* 5 n n))
Enter fullscreen mode Exit fullscreen mode

Give concise mathematical definitions for the functions computed by the procedures f, g, and h for positive integer values of n. For example, (k n) computes 5n25n^2 .

Answer:

(A 1 10) ; 1024
(A 2 4)  ; 65536
(A 3 3)  ; 65536
(define (f n) (A 0 n)) ; computes 2n
(define (g n) (A 1 n)) ; computes 2^n unless n is 0
(define (h n) (A 2 n)) ; computes 2^(2^(2^(2...(n times)))) unless n is 0
Enter fullscreen mode Exit fullscreen mode

Discussion (0)