This midterm was taken in class on February 13, 1998. The original questions appear in black text, correct solutions appear in green, and other comments, in blue.
1. (20 points.) Define and provide a descriptive example of each of the following concepts in the context of programming languages:
2. (20 points.) Reduce the lambda calculus expression,
((lambda (x) (x (x y))) ((lambda (w) w) z))
showing each step, using
==> (((lambda (w) w) z) (((lambda (w) w) z) y)) ==> (z (((lambda (w) w) z) y)) ==> (z (z y))
==> ((lambda (x) (x (x y))) z) ==> (z (z y))
3. (20 points.) Describe what the following Scheme procedure does.
(define mystery
(lambda (x)
(letrec ((loop (lambda (x y)
(if (null? x)
y
(let ((temp (cdr x)))
(set-cdr! x y)
(loop temp x))))))
(loop x '()))))
What is the value of (mystery '(1 2 3))? (3
2 1)
4. (20 points.) Use continuations to write a Scheme implementation of the procedure list-index. Specifically, if s occurs in the list of symbols los, then (list-index s los success failure) should return the value of (success i), where i is the zero-based index of the first occurrence of s in los. If s does not occur in los then (failure) should be returned.
(define list-index
(lambda (s los success failure)
(letrec ((aux (lambda (i los)
(cond ((null? los) (failure))
((eq? s (car los)) (success i))
(else (aux (+ i 1) (cdr los)))))))
(aux 0 los))))
5. (20 points.) The Scheme procedure (sproc n)
accepts a single integer argument n and returns a well defined
value after several seconds of cpu time. Write an equivalent Scheme procedure
(fproc n) that maintains within an internal cache the input
and output values of its last invocation. If the current input, n,
equals the last input value, then the last output value should be returned
without delay. Otherwise, the value of (sproc n) should
be returned, and the cache should be updated. While writing fproc,
ensure that the cache cannot be accessed or modified by any other procedures.
(define fproc
(let ((last-input '())
(last-output '()))
(lambda (n)
(if (eq? n last-input)
last-output
(begin (set! last-input n)
(set! last-output (sproc n))
last-output)))))