CS212 Exams
Spring 1998 -
Final

Destructive (erm, Imperative) Programming


  1. Write a program append! that takes two lists, and if the first is nonempty, appends the second to the first by destructive modification. Your procedure should not use pair, cons, or any other operation that creates new cons cells, and should return the result of the append. For example,
    ==> (define a '(4 5 6))
    ==> (define b '(1 2 3))
    ==> (append! a b)
    (4 5 6 1 2 3)
    ==> b
    (1 2 3)
    ==> a
    (4 5 6 1 2 3)




  2. What happens when we run the following code?
    (define (length x)
      (if (null? x) 0 (+ 1 (length (tail x)))))
    
    (define a '(1 2 3))
    
    (length (append! a a))




  3. Give a definition of zardoz that causes the expression given below to evaluate to 42 each time it is evaluated.
    ==> (* (zardoz) (zardoz))
    42

    Warning: Solutions involving do not work, because the result must be an integer. Hint: there is a nice solution involving the number 13.





Solution

Return to CS 212 Final - Spring 1998