CS212 Exams
Spring 1999 -
Final

Coding with Style


The following code fragments were from problem sets or exams that your classmates handed in earlier this semester. (The identifiers have been changed to protect the innocent.) For each code fragment, explain why the code is either inefficient, inelegant, or both, and suggest how the student should have written the code.

  1. (define (foo x) (if x x #f))
  2. (define (baz z) (map (lambda (x) (add1 x)) z))
  3. (define (blah w) (append (list 3) w))
  4. (define (bar x)
      (if (= (length x) 0)
          (error "list is empty!")
          (head x)))
  5. (define (f q x)
      (if (empty? x)
          #f
          (if (empty? (tail x))
              #f
              (if (q (head x) (head (tail x)))
                  #t
                  (if (empty? (tail (tail x)))
                      #f
                      (f q (tail x)))))))
  6. (define (g x)
      (if (> (length x) 200) (- (length x) 1) 0))

Solution

Return to CS 212 Final - Spring 1999