Sample Questions: SICP Section 1.3

Functional Abstractions in Scheme

Concept: procedures as arguments

  1. Suppose I wish to pass function y to function z. The correct syntax would be:
    1. (z (y))
    2. (z y)
    3. ((z (y)))
    4. z((y))
    5. (z)(y)
    6. z(y)
  1. Consider the following generalization of the accumulate pattern:
        (define (a b c d e f)
            (if (= b c) e (f b (a (d b) c d e f)))
            )
    
    Suppose I wish to compute the sum of the numbers from b (inclusive) to c (exclusive). What should the fifth formal parameter, f, be bound to?
    1. the + operator
    2. the number c
    3. zero
    4. a function equivalent to (define (x) (+ x 1))
    5. one
  1. Consider the following generalization of the accumulate pattern:
        (define (a b c d e f)
            (if (= b c) e (f b (a (d b) c d e f)))
            )
    
    Suppose I wish to compute the sum of the numbers from b (inclusive) to c (inclusive). What should the fifth formal parameter, f, be bound to?
    1. a function equivalent to (define (x) (+ x 1))
    2. one
    3. zero
    4. the number c
    5. the + operator
  1. Consider the following generalization of the accumulate pattern:
        (define (a b c d e f)
            (if (= b c) e (f b (a (d b) c d e f)))
            )
    
    Suppose I wish to compute the sum of the numbers from b (inclusive) to c (exclusive). What should the fourth formal parameter, e, be bound to?
    1. one
    2. the + operator
    3. the number c
    4. zero
    5. a function equivalent to (define (x) (+ x 1))
  1. Consider the following generalization of the accumulate pattern:
        (define (a b c d e f)
            (if (= b c) e (f b (a (d b) c d e f)))
            )
    
    Suppose I wish to compute the sum of the numbers from b (inclusive) to c (inclusive). What should the fourth formal parameter, e, be bound to?
    1. the number c
    2. the + operator
    3. one
    4. zero
    5. a function equivalent to (define (x) (+ x 1))
  1. Consider the following generalization of the accumulate pattern:
        (define (a b c d e f)
            (if (= b c) e (f b (a (d b) c d e f)))
            )
    
    Suppose I wish to compute the sum of the numbers from b (inclusive) to c (exclusive). What should the third formal parameter, d, be bound to?
    1. zero
    2. one
    3. the number c
    4. the + operator
    5. a function equivalent to (define (x) (+ x 1))
  1. Consider the following generalization of the accumulate pattern:
        (define (a b c d e f)
            (if (= b c) e (f b (a (d b) c d e f)))
            )
    
    Suppose I wish to compute the sum of the numbers from b (inclusive) to c (inclusive). What should the third formal parameter, d, be bound to?
    1. zero
    2. the + operator
    3. the number c
    4. a function equivalent to (define (x) (+ x 1))
    5. one
  1. Consider the following generalization of the accumulate pattern:
        (define (a b c d e f)
            (define (iter x y)
                 (if (= y c)
                     x
                     (iter (f x y) (e y))
                     )
                 )
            (iter d b)
            )
    
    Suppose I wish to compute the sum of the numbers from b (inclusive) to c (exclusive). What should the fifth formal parameter, f, be bound to?
    1. the + operator
    2. one
    3. zero
    4. a function equivalent to (define (x) (+ x 1))
    5. the number c
  1. Consider the following generalization of the accumulate pattern:
        (define (a b c d e f)
            (define (iter x y)
                 (if (= y c)
                     x
                     (iter (f x y) (e y))
                     )
                 )
            (iter d b)
            )
    
    Suppose I wish to compute the sum of the numbers from b (inclusive) to c (inclusive). What should the fifth formal parameter, f, be bound to?
    1. a function equivalent to (define (x) (+ x 1))
    2. the number c
    3. the + operator
    4. zero
    5. one
  1. Consider the following generalization of the accumulate pattern:
        (define (a b c d e f)
            (define (iter x y)
                 (if (= y c)
                     x
                     (iter (f x y) (e y))
                     )
                 )
            (iter d b)
            )
    
    Suppose I wish to compute the sum of the numbers from b (inclusive) to c (exclusive). What should the fourth formal parameter, e, be bound to?
    1. zero
    2. one
    3. the + operator
    4. a function equivalent to (define (x) (+ x 1))
    5. the number c
  1. Consider the following generalization of the accumulate pattern:
        (define (a b c d e f)
            (define (iter x y)
                 (if (= y c)
                     x
                     (iter (f x y) (e y))
                     )
                 )
            (iter d b)
            )
    
    Suppose I wish to compute the sum of the numbers from b (inclusive) to c (exclusive). What should the third formal parameter, d, be bound to?
    1. zero
    2. the + operator
    3. one
    4. the number c
    5. a function equivalent to (define (x) (+ x 1))
  1. Consider the following generalization of the accumulate pattern:
        (define (a b c d e f)
            (define (iter x y)
                 (if (= y c)
                     x
                     (iter (f x y) (e y))
                     )
                 )
            (iter d b)
            )
    
    Suppose I wish to compute the sum of the numbers from b (inclusive) to c (inclusive). What should the third formal parameter, d, be bound to?
    1. zero
    2. the + operator
    3. the number c
    4. a function equivalent to (define (x) (+ x 1))
    5. one

Concept: lambdas and lets

  1. (4 pts) Perform a syntactic transformation to replace the local defines with a let:
        (define (f x)
            (define y (- x 1))
            (define z (+ x 1))
            (+ (* x y) (* x z) (* y z))
            )
    
  1. (4 pts) Perform a syntactic transformation ro replace the local defines with a lambda:
        (define (f x)
            (define y (- x 1))
            (define z (+ x 1))
            (+ (* x y) (* x z) (* y z))
            )
    
  1. (4 pts) Perform a syntactic transformation to replace the lambda with local defines:
        (define (f x)
            ((lambda (a b c)
               (+ (* a b) c)
               )
               x (* x x) (* x x x))
            )
    
  1. (4 pts) Perform a syntactic transformation to replace the lambda with a let:
        (define (f x)
            ((lambda (a b c)
               (+ (* a b) c)
               )
               x (* x x) (* x x x))
            )
    
  1. (4 pts) Perform a syntactic transformation to replace the let with a lambda:
        (define (f x)
            (let
                ((w (* x 2))
                 (y (+ x 2))
                 (z (/ x 2))
                 )
               (+ (* w y) z x)
               )
            )
    
  1. (4 pts) Perform a syntactic transformation to replace the replace the let with local defines:
        (define (f x)
            (let
                ((w (* x 2))
                 (y (+ x 2))
                 (z (/ x 2))
                 )
               (+ (* w y) z x)
               )
            )
    

Concept: procedures as return values

  1. What does the following expression evaluate to?
        ((lambda (x) ((lambda (y) (- x y)) 10)) 6)
    
    1. -4
    2. 4
    3. an error (int called as a function)
    4. a function with formal parameter x
    5. a function with formal parameter y
  1. What does the following expression evaluate to?
        (lambda (x) ((lambda (x y) (- x y)) 10 6))
    
    1. a function with formal parameter x
    2. a function with formal parameter y
    3. 4
    4. an error (int called as a function)
    5. -4
  1. What does the following expression evaluate to?
        ((lambda (x) (((lambda (y) (- x y)) 10))) 6)
    
    1. a function with formal parameter x
    2. a function with formal parameter y
    3. 4
    4. an error (int called as a function)
    5. -4
  1. What does the following expression evaluate to?
        ((lambda (x) (lambda (y) (- x y))) 10)
    
    1. an error (int called as a function)
    2. 4
    3. -4
    4. a function with formal parameter y
    5. a function with formal parameter x