Computer Science 115, Fall 1996, Final Exam

Wednesday, December 11, 1996.

This exam contains 10 problem worth between 5 and 15 points each. There are a total of 100 points on this exam. Please make sure you have all 4 pages and all 10 problems. Please put your name on the first page you hand in, and your initials on all other pages. Please staple the pages together before handing them in.

Problem 1. (5 points)

Draw Box and Pointer diagrams for the following list structures: (Note, the ``.'' in b is the result of a dotted pair, not a period.)
a.  ((4 5) (3 9))       
b.  (17  ((9)) . 5)     

Problem 2. (5 points)

Write out the rules for the environment model of evaluation. Include the notes about "define" and "let".

Problem 3. (15 points)

Draw environment diagrams, including box and pointer diagrams, to show how Macgambit evaluates the following interaction. What return values have been replaced by the question marks?
: (define smudge 18)
: (define joon (cons 3 4))

: (define (hugin smudge pinky)
      (lambda (simone)
        (set! smudge (- smudge simone))
        (set-car! pinky (- smudge simone))
        (+ (car pinky) (cdr pinky))))

: (define simone (hugin smudge joon))

: (simone 5)

????? BLANK A ?????

: joon

????? BLANK B ?????

: smudge

????? BLANK C ?????

Problem 4. (15 points)

A typical strategy in implementing a programming language is to reduce the number of primitives in the language. For example, in Scheme let is unnecessary: any use of let can be replaced by an appropriate lambda. For example:
     : (expand-let '(let ((x 2)
                          (y 1))
                       (+ x y)))
should return
     ((lambda (x y) (+ x y)) 2 1).
Write a procedure expand-let that takes as input a Scheme let expression, and returns the equivalent procedural form in which the top-level let has been eliminated. You may assume that the input is a valid let expression.

Problem 5. (15 points)

Write the procedure (decode bits huffman-tree). This procedure takes two arguments: bits is a list of 0's and 1's, and huffman-tree is a huffman tree. The procedure should return the list of symbols encoded by bits using the huffman tree. You need not do any error checking. You may assume the following code is defined already:
;; representing huffman trees

(define (make-leaf symbol weight)
  (list 'leaf symbol weight))

(define (leaf? object)
  (eq? (car object) 'leaf))

(define (symbol-leaf x) (cadr x))

(define (weight-leaf x) (caddr x))

(define (make-code-tree left right)
  (list left
        right
        (append (symbols left) (symbols right))
        (+ (weight left) (weight right))))

(define (left-branch tree) (car tree))

(define (right-branch tree) (cadr tree))

(define (symbols tree)
  (if (leaf? tree)
      (list (symbol-leaf tree))
      (caddr tree)))

(define (weight tree)
  (if (leaf? tree)
      (weight-leaf tree)
      (cadddr tree)))

Problem 6. (5 points)

In Problem 5, let In terms of the variables above, how long (in terms of theta notation) does it take your procedure (decode bits huffman-tree) to run?

Problem 7. (10 points)

In software-testing applications, it is useful to be able to count the number of times a given procedure is called during the course of a computation. Write a procedure make-monitored that takes as input a procedure f of one input. The result returned by make-monitored is a procedure of one argument, say mf, that keeps track of the number of times it has been called by maintaining an internal counter. If the input to mf is the special symbol how-many-calls?, then mf returns the value of the counter. If the input is the special symbol reset-count, then mf resets the counter to zero. For any other input, mf returns the result of calling f on that input and increments the counter. For example:
 
: (define s (make-monitored sqrt))

: (s 100)
10

: (s 'how-many-calls?)
1

Problem 8. (10 points)

  1. Define a procedure mul-streams, analagous to the add-streams procedure we saw in class, which takes two streams as arguments and returns the stream consisting of the element-wise product of its two input streams.

  2. Complete the following definition of the stream whose nth element (n = 0, 1, 2, ...) is n factorial:
    : (define factorials 
          (s-cons 1 
                  (mul-streams  
    			   )))
    

Problem 9. (10 points)

Consider the following code:
: (define y 5)

: (define (foo x)
    (set! x 3)
    x)

: (foo y)
3

: y
5
Notice that the value of y is unchanged. In some cases, it is desirable for the value of y (or whatever variable is passed in a call to "foo") to be altered by "foo". In this case, the argument to "foo" is called a variable parameter You are to write a data structure that will implement this:

Define a constructor (make-vp x) ("vp" stands for variable-parameter, "x" is a number), a selector (value vp), and a mutator (set-vp! vp y) (this sets "vp" to "y") to implement variable parameters. For example, your data structure would allow the following interaction:

: (define y (make-vp 5))

: (value y)
5

: (define (foo vp)
    (set-vp! vp 3)
    (value vp))

: (foo y)
3
: (value y)
3

Problem 10. (10 points)

Write a procedure (cycle? x) that detects if there is any cycle whatsoever in list structure x. The cycle may involve taking CARs, CDRs, or any combination of them.