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, May 7

Final Review Handout gif

Iterative vs. Recursive Processes and Order of Growth

Consider the two procedures below. Assume the arguments passed in are always positive integers.

Given the above definitions, when the expression (odd? 24) is evaluated, does that generate a recursive or iterative process? Why?

What is the order of growth of in time of the procedure even??

What is the order of growth of in space of the procedure odd??

Change one of the two functions above to make the process the opposite of what it currently is (eg. If it's a recursive process, make it iterative and if it's an iterative process, make it recursive).

Consider the function below:

Does the function power-of-2? generate a recursive or iterative process? Why?

What is the order of growth of in time of the procedure power-of-2??

What is the order of growth of in space of the procedure power-of-2??

Higher Order Procedures and Tree Structures

We'd like to write an evaluator for simple in-fix numerical expressions. For example, consider the trees generated by the following expressions. Careful: Notice that the operators are not quoted.

How does exp1 print when evaluated?

Draw the box and pointer diagram for the tree structure of exp2

Write a function infix that evaluates these types of expressions. For example,

Consider a function infix->prefix that takes an infix expression like exp1 and exp2 above, and transforms the expression into prefix form (e.g. it swaps the order of the operator and first operand). For example, the expression (infix->prefix (list (list 3 * 2) + 4)) will produce the same list structure as (i.e. will be equal? with) the value of following expression:
(list + (list * 3 2) 4).

Write the function infix->prefix.

Local State

Assume scheme has the following special form time, which takes one argument and times how long it takes to evaluate the argument. Time returns a list of two elements, the first is the amount of time it took to evaluate the expression, and the second is the value of the expression. For example,

Assume, for simplicity, that (sqrt x) always takes 20 time units to compute and (sqr x) always takes 10 time units to compute for all positive numbers.

Louis has a program that he wants to make run faster. He wants to see what procedures are eating up the most amount of time. He decides to write a procedure called make-timed-procedure which takes a procedure and returns a procedure that does the same thing as the original, but also keeps track of the total time spent in the procedure. For example,

Louis writes the following procedure:

After defining the following two functions, Louis trys out his code. Show the output of each of the following expressions, assuming that each call to sqrt takes 20 and each call to sqr takes 10 time units.

This isn't quite the behavior we wanted. What change needs to be made to make-timed-procedure so that we get the correct behavior?

Object Oriented Programming and Environment Diagrams

Below is the object oriented system from the March 19th Lecture Notes (just included for reference)

Consider the following expressions:

Draw the environment diagram for the above three expressions.

Write the expression that you would use to get the value of counter c1.

Write the expression that you would use to increment counter c1.

Streams and Higher Order Procedures

In this part, we are going to create an infinite stream of higher-order procedures. First, here are some simple functions that we are going to be using.

Now, Consider the function compose-fstreams that takes two streams-of-functions, fs1 and fs2, and returns another stream-of-functions where each element is the result of composing the corresponding elements of fs1 and fs2.

We define the following two infinite streams of functions, one that increments and one that squares.

We can now define another infinite stream-of-functions fs (Hint: this is similar to how we defined integers in terms of ones).

What is the first element of the stream fs?

What is the second element of the stream fs?

Consider the function apply-fstream and the definition of the stream s, below.

What are the first 10 elements of the stream s?

Consider the following two streams that are defined.

What are the first 5 elements of the stream t1?

What are the first 5 elements of the stream t2?

Meta Circular Evaluator

We would like to introduce a new special form to our evaluator called same?. Same? always takes three arguments, and returns #t if all three arguments are eq?. Same?, however is smart in that if the first two arguments are different, then the third argument is not evaluated. Here are some examples of using same?.

To add this special form to the evaluator, we need to define some data abstraction.

Define the function same?? that checks to see if an expression is a same? expression.

Define the functions same?-first and same?-second that select out the first and second sub-expressions (assume someone else defined same?-third).

Next, write the appropriate clause to add to the cond clause of eval, assuming that we have the function eval-same? that will evaluate a same? expression.

Finally, write the eval-same? function that takes a same? expression and an environment and implements the special form as described above.

Fill in the blanks:

We need to make same? a special form in our language because our language has

evaluation. If, instead, our language had

evaluation, then we could simply define same? as a function.

Assuming our Scheme has the alternative method of evaluation (stated in the previous paragraph), define same? as a function.

Explicit Control Evaluator

The special form same? from the previous example was so helpful that we decided to add it to the explicit control evaluator. Assume that in addition to the registers we've used in the past, we've also got the register TMP. Fill in the blanks in the following code that evaluates a same? expression.

Does this same? operation handle tail recursion? Why or why not?

For example, is the following recursive or iterative?

Concurrency

Consider the following definitions of x and y

In the table below, list all possible final values of z after the following expressions are evaluated.

Implementing Put and Get

Assume that we have one global put and get table called *global-put-table*. Define (a simplified version of) put and get as followsgif.

For example,

Complete the following three definitions for put and get.

Register Machines

Draw the Data Paths AND Controller for a machine to compute whether the input x is odd by successively subtracting 1 from the input. You can assume that the input will be a positive integer. Assume the only primitive operations you have are subtraction, logical not, and testing equality. After your machine finishes running, the result in the answer register should be either #t or #f.

Compilers

Consider the following compiled code:

Decompile the following groups of code. (Hint: Don't worry if the overall code looks a bit unusual.)

Lines 8--22:

Lines 25--33:

Lines 8--45:

Lines 1--48:





next up previous
Next: About this document



Michael E. Leventon
Tue May 4 16:34:31 EDT 1999