next up previous
Next: About this document

MASSACHVSETTS INSTITVTE OF TECHNOLOGY
Department of Electrical Engineering and Computer Science
6.001---Structure and Interpretation of Computer Programs
Spring Semester, 1999

Recitation -- Friday, February 19

1. Abstraction using Higher Order Procedures

Let's take a look at using abstraction on common patterns.

3. Higher Order Procedures

Write a function swap that takes a function f, and returns a function, that takes two arguments, and returns f with the variables swapped: (f x y) == ((swap f) y x) For example, ((swap -) 4 5) 1.

2. Composing Procedures

Now try to write the function compose that takes two functions, f and g, and returns a function, that takes one argument, and composes f and g on that argument.

Let's trace through the evaluation of the following expression:

Notice that there's no magic here. We just used the same rules for evaluation that we've been using all along -- the substitution model!

Using compose, define the function ^5/2 which takes a number x and computes .

0.1in

5. Repeated Composition of Procedures

We saw how to compose two procedures to produce another procedure. For example, we can define the following.

Let's write a (very strange) function called repeated that takes a function f and an integer n, and composes f, n times. For example:

Let's look at a simple example:

6. Iterative Repeated

Guess what.. Now that we've written the recursive version of repeated, let's write the iterative version.

7. More Higher-Order Procedures

Write a function snoc that takes two arguments a and b and returns a function, that when called with #t returns a and when called with #f returns b.

Here's an even more elegant (albeit more obscure) way of doing the same thing. Can you figure out how this is working?





next up previous
Next: About this document



Michael E. Leventon
Fri Mar 19 17:45:15 EST 1999