Sample Questions: SICP Section 2.1

Data Abstraction

Rational numbers

  1. Suppose the rational constructor make-rat could be passed both integers and rational numbers. How could the constructor identify the type of its arguments in order to simply its result? Choose the most complete answer.
    1. use integer? or pair?
    2. use pair?
    3. use integer?
    4. neither
  1. Suppose the rational constructor make-rat could be passed both integers and rational numbers. How many cases must the constructor handle in order to simply its result?
    1. 3
    2. 4
    3. 1
    4. 6
    5. 2
    6. 7
    7. 5
    8. 8
  1. Suppose the rational constructor make-rat could be passed both integers and rational numbers. Define an expression that the constuctor could use to simpify the resultion numerator in the rational-rational case. Use the variables nn and dn to refer to the numerator of the numerator and the denominator of the numerator, respectively. Likewise for nd and dd.

Implementing fundamental types

  1. With this implementation for cons:
        (define (cons a b) (lambda (w) (w a b)))
    
    which of the following would be a valid implementation of car?
    1. (define (car c) (lambda (x y) x))
    2. (define (car c) (lambda (c) (c (lambda (x y) x))))
    3. (define (car c) (c (lambda (x y) x)))
    4. (define (car c) (c c))
  1. With this implementation for cons, which only works for integers:
        (define (cons a b) (* (^ 2 a) (^ 3 b)))
    
    which of the following would is a valid implementation of car? Assume ^ is the exponentiation function and that when given two integers, returns an integer. Assume (ilog b d) returns the closest integer to l o g b d.
    1. (define (car x) (ilog x 2))
    2. (define (car x) (if (= (% x 2) 0) 0 (+ 1 (car (/ x 2)))))
    3. (define (car x) (if (= (% x 2) 0) (ilog 3 x) (car (/ x 2))))
    4. (define (car x) (if (!= (% x 3) 0) (ilog 2 x) (car (/ x 3))))
  1. Given the following incrementer and base:
        (define (inc x) 1)
        (define base (lambda (y) (+ 1 y)))
    
    and the evaluation of the Church numbers four:
        (define x ((four inc) base))
    
    What is the value of x?
    1. (lambda (y) (+ 1 y))
    2. 4
    3. 5
    4. 1
  1. Which are valid functions for adding two Church numerals?
        (define (add a b)
            (lambda (f)
               (lambda (x)
                   ((((a add-1) b) f) x)
                   )
                )
            )
    
        (define (add a b)
            (lambda (f)
                (lambda (x)
                    ((a f) ((b f) x))
                    )
                )
            )
    
    1. both
    2. the second
    3. the first
    4. neither