6.001 Problem Set 5Fall 1990		1


MASSACHUSETTS INSTITUTE OF TECHNOLOGY
Department of Electrical Engineering and Computer Science
6.001Structure and Interpretation of Computer Programs
Spring Semester, 1990

Problem Set 5

Issued: Tuesday, October 16, 1990
Written solutions: due in Recitation on October 26
Tutorial preparation: for week of October 22

Reading Assignment:  sections 3.1 and 3.2 

Quiz announcement:  Quiz 1 is on Monday, October 22, in Walker Memorial 
Gymnasium (50340) from 57 PM or 79 PM.  You may take the quiz during either one 
of these two periods, but students taking the quiz during the first period will not be 
permitted to leave the room until the end of the period.

The quiz is closed book, but you may bring with you one 8 1/2 by 11 inch sheet of paper 
on which you have written notes.

The quiz will cover all material assigned through problem set 4, and presented in class 
through recitation on October 5.  Questions on the quiz will be strongly based on the 
problems that were assigned for written homework and for tutorial.  The best way to study 
for the quiz is to review the homework and tutorial assignments.

Because of the quiz, there is no programming assignment due for this problem set.

Tutorial preparation:

For tutorials the week of October 22, you should be prepared to discuss the following 
exercises about state and environments:

Tutorial Problem 1: Do exercise 3.11 on page 198 of the textbook.

Tutorial Problem 2:  Write a procedure flip (of no parameters) that returns 1 the first time it is 
called, 0 the second time it is called, 1 the third time, 0 the fourth time, and so on:

==> (flip)
1
==> (flip)
0
==>(flip)
1
==>(flip)
0

Written homework

To solidify your understanding of environment diagrams, write up answers to tutorial problems 1 
and 2 (even though you will have already discussed them in tutorial).

In addition, answer the following problems:

Problem 3:  Define a procedure every-nth that takes an integer n as its argument.  
The result should be a procedure  (of no arguments) that returns 1 every nth time it is 
called, and 0 the rest of the time.  For example, (every-nth 2) should return a 
prcedure that behaves just like flip in problem 2.

Problem 4:  Write a definition that produces the following environment diagram:


                                


Problem 5:  Suppose we want to have a procedure c that  behaves as a counter

(c) --> 0
(c) --> 1

together with a procedure reset that resets the count to 0:

(c) --> 2
(reset) --> OK
(c) --> 0
(c) --> 1
...

The idea is that c and reset should share some local state, but that no other procedures 
should be able to access this state.  Draw an environment diagram that illustrates this 
situation.  (Assume the the local state variable is called count.)  How could you create the 
procedures c and reset ? 



