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

@Comment(

Copyright (c) 1990 Massachusetts Institute of Technology

This material was developed by the Scheme project at the Massachusetts
Institute of Technology, Department of Electrical Engineering and
Computer Science.  Permission to copy this material, to redistribute
it, and to use it for any non-commercial purpose is granted, subject
to the following restrictions and understandings.

1. Any copy made of this material must include this copyright notice
in full.

2. Users of this material agree to make their best efforts (a) to
return to the MIT Scheme project any improvements or extensions that
they make, so that these may be included in future releases; and (b)
to inform MIT of noteworthy uses of this material.

3. All materials developed as a consequence of the use of this
material shall duly acknowledge such use, in accordance with the usual
standards of acknowledging credit in academic research.

4. MIT has made no warrantee or representation that this material
(including the operation of software contained therein) will be
error-free, and MIT is under no obligation to provide any services, by
way of maintenance, update, or otherwise.

5. In conjunction with products arising from the use of this material,
there shall be no use of the name of the Massachusetts Institute of
Technology nor of any adaptation thereof in any advertising,
promotional, or sales literature without prior written consent from
MIT in each case. 

)

@device(dover)
@make(6001)

@modify(excounter, numbered [Exercise @1], referenced [@1])

@PageHeading(left "6.001 -- Fall Semester 1985",
             center "@Value(Page)",
             right "Problem set 1")

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

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

@begin(flushleft)
Issued: Tuesday, September 10

Due:
@begin(itemize)
on Wednesday, September 18
for recitations meeting at 9:00, 10:00, and 11:00

on Friday, September 20
for recitations meeting at 12:00, 1:00, and 2:00
@end(itemize)

Reading: From text, Chapter 1, section 1.1.
@end(flushleft)

Problem sets should always be handed in at recitation.  Late work will
not be accepted.  As explained in the Course Organization handout, we
are staggering the due-dates for problem sets, so as to
minimize crowding in the lab.  The Wednesday/Friday due dates will be
reversed during the second half of the semester to assure equitable
treatment of students in both morning and afternoon recitations.
specific due dates will be announced as each problem set is handed
out.

Before you can begin work on this assignment, you will need to buy
a copy of the textbook and to pick up
the manual package.  To obtain the manual package, take the coupon
attached, and follow the instructions
given in the handout marked ``6.001: Course Organization.''

@section(Homework exercises)

Write up and turn in the following exercises from the text:
@begin(itemize)

@begin(multiple)
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.
@end(multiple)

Exercise 1.2: defining a simple procedure

Exercise 1.3: a test to determine evaulation order

Exercise 1.4: new definition of @a[if]
@end(itemize)

@section(Programming Assignment: Graphing Lissajous figures)


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.

Before coming to the laboratory room, you should read the ``Chipmunk
Manual'' 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.''

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:
@begin(example)
   x(t) = A@-[1]sin(f@-[1]t)          y(t) = A@-[2]sin(f@-[2]t + p)

@end(example)
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 @a[A1] and @a[A2], and frequencies @a[f1] and @a[f2], and
the phase difference @a[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 @a[DT] that determines the increment at
which points are plotted.
@begin(programexample)
(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
@end(programexample)          

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

@begin(exercise)
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 begin 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 parenthesis, the matching left parenthesis 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.
You need not turn in anything for this part.
@end(exercise)

@begin(exercise)
``Zap'' your procedure from the editor into Scheme,@foot{Use the
@c[send buffer] command (@c[shift]-k@-[7] on the Chipmunk keyboard).
This transmits the entire buffer to Scheme, and is the easiest thing
to use when you only have one or two procedures in your buffer.  With
larger amounts of code, it is better to use other ``zap'' commands,
which transmit only selected parts of the buffer, such as individual
procedure definitions.  For a description of these commands, see the
description of NMODE in the Chipmunk manual, under the heading
``Commands for use with Scheme.''} and run it
with test values by executing:
@begin(programexample)
(lissajous 150 150 1 1 0.2 0.05)
@end(preogramexample)

(Press @c[EXECUTE] after typing in the form.)  This should draw an
ellipse.  To see the drawing, press the key marked @c[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 @c[GRAPHICS] and @c[ALPHA].  Pressing
@c[GRAPHICS] shows the graphics screen superimposed on the text.  Pressing
@c[GRAPHICS] again hides the texts screen and shows only graphics.  @c[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 @a[lissajous] procedure does not terminate.  It will keep
running until you abort execution by typing control-G.  (The @c[CONTROL]
key at the upper left of the keyboard is used like a @c[SHIFT] key.  To
type control-G, hold down the @c[CTRL] key and press G.)

You need not turn in anything for this part.
@end(exercise)

@begin(exercise)
Define a procedure @a[ampl-mult] that takes as argument a number
@a[m], and calls @a[lissajous] with @a[a1] equal to 50, A2 equal to
@a[m] times 50, @a[f1] and @a[f2] both 1, @a[p] equal to @a[pi/2] and
@a[dt] as .05.

In order to do this conveniently, define in Scheme @a[pi] to be 3.14159,
and use this value to define @a[pi/2].

Explore the family of figures generated by @a[ampl-mult] as @a[m] varies
smoothly from -2 to 2.

By modifying @a[ampl-mult], perform the same analysis for figures with
@a[f1=1] and @a[f2=3].

Turn in sketches of the figures, together with a listing of the
procedure @a[ampl-mult], and the expressions you typed to Scheme to define
@a[pi], @a[pi/2], and @a[2pi].

In doing this problem, you can save typing by taking advantage of the
``line edit'' and ``recall'' features of Scheme.  Pressing the
@c[recall] key will redisplay the last Scheme expression evaluated.
This can then be edited using many of the NMODE edit commands.@foot{In
general, you can use all the edit commands that keep the cursor within
a single line.  An exception: Do not use @c[ctrl]-B if you want to
move the cursor to the left.  Use the left-arrow key.} For instance,
suppose you have just evaluated @a[(ampl-mult 0.1)] and you now
want to evaluate @a[(ampl-mult 0.2)].  You can press @a[recall],
use the arrow keys to move the cursor to the character to be changed,
make the edit, and finally press @c[execute] to evaluate the modified
expression.  @c[Recall] always redisplays the previous expression.
Once you have pressed @c[recall], you can repeatedly press
@c[ctrl-recall] to replace this by the next-to-last expression, the
expression before that, and so on, cycling through a ``ring buffer''
of saved expressions.
@end(exercise)

@begin(exercise)
Define a procedure @a[phase-shift], which takes as arguments numbers
@a[f1], @a[f2], and @a[p], and calls @a[lissajous] with @a[a1] and
@a[a2] both equal to 150, @a[f1], @a[f2], and @a[p] as specified, and
@a[dt] equal to 0.05.  This corresponds to using an oscilloscope to
observe a sinusoid plotted against the same signal, shifted by a given
phase.  Using this procedure, explore the family of figures generated
for @a[f1=1] and @a[f2=2], What does the resulting figure look like
for @a[p=0]? for @a[p=pi/2]? for @a[p=pi]? for intermediate values of
@a[p]?

In addition to sketches of the figures, turn in a listing of the
procedure @a[phase-shift].
@end(exercise)

@begin(exercise)
Examine the Lissajous figures formed by signals with
different frequencies: @a[f1=2] and @a[f2=3];
@a[f1=5] and @a[f2=6]; @a[f1=3] and @a[f2=5.]
For some of these you may want to decrease the value of @a[dt] to obtain a
more accurate plot.  Experiment with the figures, noting how they
change as the phase shift varies from 0 to @a[pi].
@end(exercise)

@begin(exercise)
@tag(draw-once)
If we choose frequencies @a[f1] and @a[f2] that are integers
greater than 1, then the Lissajous figure will be completely drawn by
the time @a[t=2pi].  Explain why.  Use this idea to modify the @a[lissajous]
program so that it stops when @a[t>=2pi] (let the procedure return 0
when it stops).  This requires only a small modification to the @a[iter]
procedure.  Turn in a listing of your modified procedure.
@end(exercise)

@begin(exercise)
Using your modified procedure of exercise @ref(draw-once), draw a
Lissajous figure with @a[f1=6] and @a[f2=8].  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 @a[gcd], which computes the greatest common
divisor of two integers.  See section 1.2.5 of the course text.)
@end(exercise)

@begin(exercise)
Some Lissajous figures contain places where the curve ``stops'' and
the graph backs up and retraces itself.  An example is @a[f1=3],
@a[f2=5], @a[p=0].  Why does this happen?  Give some more examples of
triples @a[f1], @a[f2], and @a[p] that exhibit this behavior.  Give a
rule (in terms of @a[f1], @a[f2], and @a[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.
@end(exercise)


@include(DEBUG-LESSON.TXT)
