Solutions to Midterm #3

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:

(a) Denoted value: A denoted value is the value to which a variable is bound. In C, for example, the denoted value of a variable is its address in memory. (See pages 2 and 140, of EOPL.)
(b) Closure: A closure is a procedure that maintains the value to which every free variable is bound . (See page 152 of EOPL.)
(c) Interpreter: An interpreter is a procedure that reads an abstract syntax tree and then performs the indicated operation. Most interpreters are built within an interactive shell, that includes a scanner and a parser. The scanner accumulates the user's typed commands into tokens, the parser converts combinations of tokens into syntax trees. Examples of interpreteters include the scheme interpreter that runs unde emacs, and the simple interpreter that appears in Chapter 5 of EOPL.
(d) Stream: A sequence of symbols, or queue, possibly infinite in length, in which the symbols can be accessed in chronological order, while new symobls are acquired. Streams are often used to store input and output data, until they are accessed by a running program or the operating system. Important streams in C and Scheme are the standard input and output. (See page 131 of EOPL.)
 

2. (20 points.) Reduce the lambda calculus expression,

((lambda (x) (x (x y)))
 ((lambda (w) w) z))

showing each step, using

(a) normal order reduction, and then
==> (((lambda (w) w) z) (((lambda (w) w) z) y))
==> (z (((lambda (w) w) z) y))
==> (z (z y))
(b) applicative order reduction.
==> ((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 '()))))
 
mystery accepts a list as a single argument, and reverses the order of the cons cells in the list.

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


CS 103 Home Page
Robert Snapp's Home Page
(Last modified on April 19, 1998.)