Sample Questions: SICP Section 1.3
Functional Abstractions in Scheme
Concept: procedures as arguments

Suppose I wish to pass function y to function z. The correct syntax
would be:

(z (y))

(z y)

((z (y)))

z((y))

(z)(y)

z(y)

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?
 the
+
operator
 the number c
 zero
 a function equivalent to
(define (x) (+ x 1))
 one

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?
 a function equivalent to
(define (x) (+ x 1))
 one
 zero
 the number c
 the
+
operator

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?
 one
 the
+
operator
 the number c
 zero
 a function equivalent to
(define (x) (+ x 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?
 the number c
 the
+
operator
 one
 zero
 a function equivalent to
(define (x) (+ x 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?
 zero
 one
 the number c
 the
+
operator
 a function equivalent to
(define (x) (+ x 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?
 zero
 the
+
operator
 the number c
 a function equivalent to
(define (x) (+ x 1))
 one

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?
 the
+
operator
 one
 zero
 a function equivalent to
(define (x) (+ x 1))
 the number c

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?
 a function equivalent to
(define (x) (+ x 1))
 the number c
 the
+
operator
 zero
 one

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?
 zero
 one
 the
+
operator
 a function equivalent to
(define (x) (+ x 1))
 the number c

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?
 zero
 the
+
operator
 one
 the number c
 a function equivalent to
(define (x) (+ x 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?
 zero
 the
+
operator
 the number c
 a function equivalent to
(define (x) (+ x 1))
 one
Concept: lambdas and lets

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

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

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

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

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

(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

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

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

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

What does the following expression evaluate to?
((lambda (x) (lambda (y) ( x y))) 10)
 an error (int called as a function)
 4
 4
 a function with formal parameter y
 a function with formal parameter x