                MASSACHUSETTS INSTITUTE OF TECHNOLOGY

      Department of Electrical Engineering and Computer Science

       6.001 Structure and Interpretation of Computer Programs

                            Problem Set 1

        Issued: 13 September 1983           Due: in Recitation
                                                 21 September 1983

Problem sets will always be due in your Wednesday recitation section.
Late work will not be accepted.

Before you can begin work on this assignment, you will need to pick up
the course notes.  To obtain the course notes, take the coupon that
was distributed at the first lecture, and follow the instructions
given in the handout marked "6.001: Course Organization."

Reading: From text, Chapter 1, section 1.1.

Errata:  Exercise 1-3 is incorrect, as is the comment in the text just
before Exercise 1-1 about AND and OR being special forms.  (In the
previous version of Scheme, AND and OR were special forms.  Now they
are ordinary procedures.)

Write up and hand in the following exercises from the text.

Exercise 1-1:  Evaluation of expressions

You may use the computer to check your answers, but first do the
evaluation by hand so as to get practice with understanding Lisp.
Giving forms such as these to the interpreter and observing its
behavior is one of the best ways of learning about the language.
Experiment and try your own problems.  Though the examples shown are
often indented and printed on several lines for readability, an
expression may be typed on a single line or on several, and redundant
spaces and carriage returns are ignored.  It is to your advantage to
format your work so that you (and others) can easily read it.

Exercise 1-2:  Defining a simple procedure

Exercise 1-4:  Normal vs. applicative order evaluation

Exercise 1-7:  Computing cube roots


                        LABORATORY ASSIGNMENT

A substantial part of each problem set consists of a laboratory
assignment that should be done with the aid of the 6.001 computing
facility.  Performance on problems sets, and laboratories in
particular, are a major factor in determining grades for 6.001.
Remember that the laboratory room tends to be very crowded on the
nights before assignments are due, so it will be to your advantage to
complete your computer work early.  The assignment below consists of a
number of parts.  Some of the parts below are marked with stars.
These indicate that they contain questions to be answered and turned
in as part of the assignment.

Before coming to the laboratory room, you should read the "Chipmunk
Manual" that has been included with the course notes, and you should
bring it with you to use for reference.  You should also glance
through the "Scheme Manual," but almost all of the information you
will need about the Scheme language itself has been included in the
text.

Each time you begin using one of the Chipmunk computers, you must
insert a floppy disk in the drive.  Each disk is identified by a name,
which is chosen by you at the time when the disk is initialized.  If
you place an uninitialized disk in the drive, the system will offer to
initialize it for you.  Before beginning work on the first assignment,
you should have the system initialize your disk.  More information is
given in the Chipmunk Manual, in the section called "starting and
leaving the system."

                      GRAPHING LISSAJOUS FIGURES

This first laboratory assignment has been designed to be very simple,
so that you will have a chance to become familiar with the Chipmunk
system.

In this assignment, you are to experiment with a simple procedure
that plots curves known as Lissajous figures.  These are the figures
traced by a point that moves in two-dimensions, whose x and y
coordinates are given by sinusoids:

        x(t) = A sin(f t)               y(t) = A sin(f t + p)
                1     1                         2     2

For example, if we connect AC signals to the horizontal and
vertical deflection of an oscilloscope, the image traced will be a
Lissajous figure.  The shape of the figure is determined by the
signals' amplitudes A1 and A2, and frequencies f1 and f2, and the
phase difference p between the two signals.

The following procedure plots Lissajous figures.  The arguments to the
procedure are the amplitudes, frequencies, and phase difference of the
two sinusoids, and a parameter DT that determines the increment at
which points are plotted.

(define (lissajous a1 a2 f1 f2 p dt)
  (define (horiz t)
    (* a1 (sin (* f1 t))))
  (define (vert t)
    (* a2 (sin (+ (* f2 t) p))))
  (define (iter t)
    (draw-line-to (horiz t) (vert t))
    (iter (+ t dt)))
  (clear-graphics)                    ;clear the graphics screen
  (position-pen (horiz 0) (vert 0))   ;set graphics at initial point
  (iter 0))                           ;start drawing
          

The LISSAJOUS procedure contains subprocedures that compute the
horizontal and vertical coordinates corresponding to a given value of T,
and a procedure ITER that repeatedly draws lines connecting the given points
for consecutive values of T.  When LISSAJOUS starts, it clears the
graphics screen and positions the graphics pen at the initial point
corresponding to T=0 before calling ITER.  The primitive DRAW-LINE-TO
used by ITER, moves the graphics pen to the indicated x and y
coordinates, drawing a line from the current point.

[Part 1] Use the NMODE editor to type in the above procedure
definition.  (Subsequent laboratory assignments will include large
amounts of code, which will be loaded automatically when you being work
on the assignment.  This time, however, we are asking you to type in
the definition yourself, so that you will get used to the editor.)
Notice that the editor automatically "pretty prints" your procedure as
you type it in, indenting lines to the position determined by the
number of unclosed left parentheses.  Notice also that, when you type
a right parentheses, the matching left parentheses is briefly
highlighted.  In the code above, words to the right of semicolons are
comments, and need not be typed.  After you have finished entering the
definition, save the procedure on your disk in a file named PS1.

[Part 2]  "Zap" your procedure from the editor into Scheme, and run it
with test values by executing:

(lissajous 150 150 1 1 0.2 0.05)

(Press EXECUTE after typing in the form.)  This should draw an
ellipse.  To see the drawing, press the key marked GRAPHICS at the
upper right of the keyboard.  The Chipmunk system enables you to view
either text or graphics on the screen, or to see both at once.  This
is controlled by the keys marked GRAPHICS and ALPHA.  Pressing
GRAPHICS shows the graphics screen superimposed on the text.  Pressing
GRAPHICS again hides the texts screen and shows on graphics.  ALPHA
works similarly, showing the text screen and hiding the graphics
screen.

If you typed the program incorrectly, you will need to debug the
procedure and return to the editor to make corrections.

Observe that the LISSAJOUS procedure does not terminate.  It will keep
running until you abort execution by typing control-G.  (The CONTROL
key at the upper left of the keyboard is used like a SHIFT key.  To
type control-G, hold down the CONTROL key and press G.)

[*Part 3] Define a procedure PHASE-SHIFT that takes a phase difference
p as argument, and calls Lissajous with A1 and A2 both 150, f1 and f2
both 1, dt as .05, and p as the designated value.  This corresponds to
using an oscilloscope to observe a sinusoid plotted against the same
signal, shifted by a given phase.  What does the resulting figure look
like for p=0? for p=PI/2? for p=PI? for intermediate values of p?

In order to try this, enter Scheme and define PI to be 3.14159, and
use this value to define PI/2 and 2PI.  

In addition to the descriptions of the figures, turn in for this
question a listing of the procedure PHASE-SHIFT and the expressions
you typed to Scheme to define PI, PI/2, and 2PI.

[Part 4] Examine the Lissajous figures formed by signals with
different frequencies: f1=2 and f2=3; f1=5 and f2=6; f1=3 and f2=5.
For some of these you may want to decrease the value of DT to obtain a
more accurate plot.  Experiment with the figures, examining how they
change as the phase shift varies from 0 to PI.  Experiment also with
changing the amplitudes, to see how this affects the figures.

[*Part 5] If we choose frequencies f1 and f2 that are greater than 1,
then the Lissajous figure will be completely drawn by the time t=2PI.
Explain why.  Use this idea to modify the LISSAJOUS program so that it
stops when t=PI (let the procedure return NIL when it stops).  This
requires only a small modification to the ITER procedure.  Turn in a
listing of your modified procedure.

[*Part 6] Using your modified procedure of part 5, draw a Lissajous
figure with f1=6 and f2=9.  How many times is the figure traced before
the program stops?  Describe how to modify your program so that it
will stop after drawing the figure once.  You need consider only the
case where the frequencies are integers.  Explain the idea behind your
stop rule.  (Hint: You may want to make use of the Scheme primitive
GCD, which computes the greatest common divisor of two integers.  See
section 1.2.5 of the course text.)

[*Part 7] Some Lissajous figures contain places where the curve
"stops" and the graph backs up and retraces itself.  An example is
f1=3, f2=5, p=0.  Why does this happen?  Give a rule (in terms of f1,
f2, and p) that predicts which figures will contain such "stop
points."  Be sure to test your rule by trying various figures.
Explain why your rule works.
