Computer Science 115-03, Midterm. Friday, November 1, 1996.

There are 8 questions (worth different amounts) and 125 points possible on the exam. Do not answer any questions on this sheet. Make sure your name is on the first page of the packet you hand in. Put your initials on subsequent pages. Staple the papers together before handing in.

  1. [20 points]. What return values have been substituted for the BLANKS in the following code?
    : (define x 4)
    : (define y (cons 3 (cons x '())))
    : y
    
    ???  BLANK 1a. ???
    
    : (let ((x 3)
            (y (* x 2)))
        (+ x y))
    
    ???  BLANK 1b. ???
    
    : (cond 
         ((not (= x 4))  (error ``oops''))
         ((> x 4)        (+ x (car y)))
         ((= x 4)        (cdr y))
         (else           (* 2 x)))
    
    ???  BLANK 1c. ???
    
    : (define (sum term a next b)
        (if (> a b)
            5
            (+ (term a) (sum term (next a) next b))))
    
    : (sum (lambda (z) (* 2 z))
           10
           (lambda (z) (* 10 z))
           1000)
    
    ???  BLANK 1d. ???
    

  2. [15 points] Write a scheme procedure that takes 3 numbers as arguments and returns \#t if there are exactly 2 distinct values among them. Your first line should look like this:
    (define (twovals? a b c) ...
    

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

  4. [15 points] Suppose that a, b, and c are bound to the lists of Question 3a, 3b, and 3c repectively.
    For Question 4a, give an expression using only car, cdr, and a that returns the value 9.
    For Question 4b, give an expression using only car, cdr, and b that returns the value 9.
    For Question 4c, give an expression using only car, cdr, and c that returns the value 9.

  5. [15 points] Define a procedure reverse that takes a list as an argument and returns a list of the same elements in reverse order. For example:
    : (reverse (list 1 4 9 16 25))
    (25 16 9 4 1)
    

  6. [20 points] Write a function repeated that takes a function f, a positive integer n and a list of numbers lis, and returns the list of numbers resulting from applying f to each element of lis n times. The first line of your definition and a sample of how it should work is as follows:
    : (define (repeated f n lis) ...
    
    : (repeated (lambda (x) (+ x 1)) 3 '(4 5 6))
    
    (7 8 9)
    
    : (repeated square 2 '(0 1 2 3))
    
    (0 1 16 81)
    
    Write 2 versions of repeated: one that generates an iterative process and one that generates a recursive process. For each version, describe the time and space requirements (using Theta(...) notation). Your answer should be in terms of n ; you may assume that the lis has constant size.

  7. [20 points] Consider the following code:
    (define (test n)
      (define (pair x) (cons x x))
     (pair (random n)))
    
    Recall that (random n) returns a random number between 0 and n-1.

  • [5 points] What is the action of the interpreter in the final call below?
    : (define (f g) (g 2))
    
    : (f square)
    4
    
    : (f (lambda (z) (* z (+ z 1))))
    6
    
    : (f f)
    
    ??? WHAT DOES THE INTERPRETER DO HERE?  EXPLAIN.  ???