CS212 Exams
Spring 1999 -
Final

Solution to Coding with Style

WARNING: You should seriously try to answer this question before looking at the solution -- it's just a good study skill. Also, some answers on this page may be more brief than you would want to put down in an actual exam. If you want any type of partial credit, you should write more than just the solution.


  1. The if expression is useless. Could have just written:
    (define (foo x) x)
  2. The lambda expression used in the map is useless. Could have just written:
    (define (baz z) (map add1 z))
  3. Using append and list to construct a new list is inelegant and inefficient. Should have written:
    (define (blah w) (cons 3 w))
  4. Using length to see if the list is empty is inefficient. Should have written:
    (define (bar x)
      (if (empty? x)
          (error "list is empty!")
          (head x)))
  5. Nested ifs are inelegant. Should have written:
    (define (f q x)
      (and (pair? x)
           (pair? (tail x))
           (or (q (head x) (head (tail x)))
               (f q (tail x)))))
  6. Calculating the length twice is inefficient. Should have written:
    (define (g x)
      (let ((x-length (length x)))
        (if (> x-length 200)
            (- x-length 1)
            0)))

Question

Return to CS 212 Final - Spring 1999