CS212 Exams
Spring 1997 - Prelim 1

Higher-Order Functions


The following procedure map was introduced in lecture:
(define (map f lst)
  (if (empty? lst)
      empty
      (cons (f (head lst)) (map f (tail lst)))))
  1. Write a procedure copy-list that uses map to copy a list.





  2. Write a procedure deriv-list that uses map to apply the procedure deriv to each element of a list. Recall from lecture that deriv takes two arguments, for example (deriv square .001) returns the derivative of the square procedure computed to an accuracy of .001:
    (define (deriv f dx)
      (lambda (x)
        (/ (- (f (+ x dx))
              (f x))
           dx)))

    Deriv-list should also take two arguments: the list and the accuracy.







  3. Now consider the procedure
    (define (accumulate base combiner lst)
      (if (empty? lst)
          base
          (combiner (head lst)
                    (accumulate base combiner (tail lst)))))

    Rewrite the map procedure in terms of accumulate (i.e., define the map procedure using accumulate).








Solution

Return to CS 212 Prelim 1 - Spring 1997