@comment(Hey, EMACS, this is -*- SCRIBE -*- input)

@PageHeading(left "6.001 -- Spring Semester 1987",
             center "@Value(Page)",
             right "Problem set 8")

@begin(center)
MASSACHUSETTS INSTITUTE OF TECHNOLOGY
Department of Electrical Engineering and Computer Science
6.001 Structure and Interpretation of Computer Programs
Spring Semester, 1987

Problem Set 8
@end(center)
@blankspace(0.25 in)

@flushleft(Issued: Tuesday, 7 April 1987)

Due: In recitation on Wednesday 15 April for sections meeting at 12,
1, and 2, and in recitation on Friday 17 April for sections meeting
at 9, 10, and 11.

@b{Quiz Reminder:} Remember that Quiz 2 is on Wednesday, April 22.
The quiz will be held in Walker Memorial Gymnasium (50-340) from 5-7PM
or 7-9PM.  You may take the quiz during either one of these two periods,
but students taking the quiz during the first period will not be allowed
to leave the room until the end of the period.  The quiz is open book,
and will cover material from the beginning of the semester through the
material presented in recitation on April 7, 1987 and through the end
of Chapter 3 in the text.  

Reading Assignment: Review section 3.3

@begin(center)
@b[Exercises]
@end(center)

Write up and turn in the following exercises from the text:

@begin(itemize)

Exercise 3.12, 3.13, 3.18: @I[append!, make-cycle!]

Exercise 3.20: environment diagrams for mutable data

Exercise 3.22: representing queues as procedures with local state

@end(itemize)

@blankspace(0.1 in)

Extended Exercise (Data structures) 

Ben Bitdiddle has constructed a system, based on a two-dimensional
table (pp 215-218 of the text), for maintaining a data base of student
names organized by course-major and state of home residence.  (An
alternative solution could use the query language system, to be
discussed in Chapter 4.) The system utilizes the procedures MAKE-TABLE, 
LOOKUP, INSERT!, and ASSQ listed on pp 214-215 of the text,
together with the definitions of S-TABLE and
auxiliary procedures FIND and STORE! given below:

@Begin(ProgramExample)
(define s-table (make-table))
(define (find state course) (lookup state course s-table))
(define (store! state course student) 
  (insert! state course student s-table))
@End(ProgramExample)

a.  Is the process evolved by the procedure ASSQ iterative?
@NewPage 
Assume that Ben makes the following entries in the database:
@Begin(ProgramExample)
==> (store! 'Alabama 'Architecture 'Anderson)
==> (store! 'Alabama 'Biology 'Burns)
==> (store! 'Alabama 'Chemistry 'Curtis)
==> (store! 'Colorado 'Architecture 'Ames)
==> (store! 'Colorado 'Biology 'Bemis)
==> (store! 'Colorado 'Chemistry 'Clovis)
==> (store! 'Colorado 'Chemistry 'Crocker)
@End(ProgramExample)

b.  Draw a box-and-pointer diagram of the result of evaluating the
expression (ASSQ 'ALABAMA (CDR S-TABLE)).

c.  How many CONS cells now constitute the data structure that is the
value of the variable S-TABLE.  You must give a number and 
provide a sentence or two to support your reasoning.

d.  Ben now types the following to SCHEME:
@Begin(ProgramExample)
==> (find 'colorado 'chemistry)
@End(ProgramExample)

How many times does is the ASSQ procedure test the predicate EQ?  You
must give a number and you should provide a sentence or two to support
your reasoning. What value is returned by the SCHEME interpreter?

e.  Write a predicate STUDENT? of one argument NAME which determines
whether NAME is the name of any student in the database.  For example
(STUDENT? 'CURTIS) should return a true value, while (STUDENT? 'GRAVES) 
should return a false value.

f.  Write a procedure S-STUDENT of one argument STATE that produces a
list of all students whose home residence is STATE.  If more than one
student is from a given state, the order of the students in the list
is immaterial.  For example, in Ben Bitdiddle's data base:
@Begin(ProgramExample)
==> (s-student 'Maine)
()

==> (s-student 'Alabama)
(ANDERSON BURNS CURTIS)
@End(ProgramExample)

g.  Write a procedure C-STUDENT of one argument COURSE that produces a
list of all students enrolled in COURSE.  If more than one student is
enrolled in a given course, the order of the students in the list is
immaterial.  For example, in Ben Bitdiddle's data base:
@Begin(ProgramExample)
==> (c-student 'Mathematics)
()

==> (c-student 'Biology)
(BURNS BEMIS)
@End(ProgramExample)

h.  You may have noticed that so far, our set up can only accomodate 
one student in a particular major and a particular state.  Obviously, 
this is inadequate in most cases.  Design and implement a solution 
to this problem so that we can have any number of students in the same 
state and major.  Turn in new listings of any changes and additions 
that you make (i.e. print out your programs from above now!).  Turn in 
photo listings which prove that your system works correctly.

The correct solution will leave all previous code essentially unchanged. 
You will be penalized for unnecessary modifications. However, you 
may find it useful to rename some of the existing procedures.  In any 
case, your new code must conform to the following specifications:
@Begin{Itemize} 
FIND should take a state and a course and return a list of the 
students who are from that state and in that course.

STORE! should take a state, a course, and @U[one] student and add that 
student into the table.

S-STUDENT and C-STUDENT should return a list of the appropriate
students (not a list of lists.) 
@End{Itemize} 

i.  Inevitably, someone will make a mistake on your new system and 
put in a wrong name in S-TABLE.  Before the modification part h, 
this was not a problem since you could just STORE! the right name and 
overwrite the incorrect one.  Unfortunately, the modifications have 
eliminated the ability to remove a name. Write a procedure
REMOVE! with three arguments STATE, COURSE, and NAME and 
removes the corresponding entry if it exists.  REMOVE! should return 
the symbol OK if a name was removed, and the symbol
NOTHING-REMOVED if the entry did not exist. Turn in a 
listing of your program and a photo file which tests your REMOVE! 
under the following conditions:
@Begin{Itemize} 
The entry being removed is the first in the list.

The entry being removed is the last in the list.

The entry being removed is the only element in the list.

The entry being removed isn't in the list at all.
@End{Itemize} 

After each REMOVE!, do a FIND to show that the name was correctly 
removed.
