CS212 Exams
Fall 1996 - Prelim 1

Higher-Order Functions


This problem concerns higher-order procedures for for composing functions. Recall that a higher-order procedure is one that takes a procedure as an argument or returns one as a value.

The composition of two functions (or procedures) is simply the application of one followed by the other. Consider the following procedure:

(define (compose f g)
   (lambda (x) (f (g x))))

Thus ((compose f g) x) is equivalent to (f (g x)).

This idea can be extended to a list of functions, where ((compose-list (list f g h)) x) is equivalent to (f (g (h x))). Write a procedure compose-list that takes a list of procedures and computes the composition of those procedures. For example, your procedure should behave as follows,

((compose-list (list square inc)) 2) --> 9

((compose-list (list list head reverse)) '(1 2 3)) --> (3)

((compose-list (list inc)) 3) --> 4

((compose-list '() 3)) --> 3

Your procedure should run in time linear in the length of the list of functions. It should not use any helper procedures (including ones defined internally to compose-list) and should not use set!. You need not necessarily use the function compose defined above in your solution.







Solution

Return to CS 212 Prelim 1 - Fall 1996