Laboratory: Iterating Over Lists
Summary:
In this laboratory, you will explore techniques for iterating over lists
using the map and for-each
procedures.
Reference:
Preparation
a. If your library does not include definitions
for all the spot functions, add the definitions of
spot-new, spot-col,
spot-row, spot-color,
image-render-spot!,
image-scaled-render-spot!,
spot-htrans, and spot-vtrans
to your definitions pane. Most of these definitions can be found
at the end of the lab on
spot lists. The rest can be found at the end of this lab.
b. Load your library.
c. Create a list of six to ten spots and call it
my-stamp. If you're not feeling particularly creative,
you can use the following.
(define my-stamp
(list (spot-new 2 0 "blue")
(spot-new 0 1 "blue")
(spot-new 1 1 "blue")
(spot-new 2 1 "blue")
(spot-new 3 1 "blue")
(spot-new 4 1 "blue")
(spot-new 2 2 "blue")
(spot-new 1 3 "blue")
(spot-new 3 3 "blue")
(spot-new 1 4 "blue")
(spot-new 3 4 "blue")))
d. Add the following definitions (taken from the reading) to your
definitions pane.
Exercises
Exercise 1: Drawing the Figure
a. Create a new 200x200 image and call it canvas.
b. Using image-render-stamp!, render
my-stamp on canvas.
c. Using image-render-big-stamp!, render my-stamp on
canvas.
Exercise 2: Drawing Copies
Recall that we can horizontally translate each spot in a list
of spots using (map (lambda (spot) (spot-htrans spot
offset)) stamp)
and that you can vertically translate each spot using (map
(lambda (spot) (spot-vtrans spot offset))
stamp).
a. Confirm that (map (lambda (spot) (spot-htrans spot 7)) my-stamp)
translates each spot seven spaces to the right. That is, execute the
instruction and read the results.
b. Confirm that (map (lambda (spot) (spot-vtrans spot 11)) my-stamp) translates each spot eleven spaces down.
Once again, execute the instruction and read the results.
c. Create a new image and render each of the translated figures using
image-render-stamp!.
Exercise 3: Multiple Translations
a. Write a procedure, (stamp-htrans
stamp offset),
that translates each spot in stamp
horizontally by offset.
b. Write a procedure, (stamp-vtrans
stamp offset),
that translates each spot in stamp
vertically by offset.
c. Write a procedure, (stamp-translate
stamp hoffset
voffset), that translates each
spot in stamp horizontally by
hoffset and vertically by
voffset.
Exercise 4: Safely Drawing Copies
a. Create a 200x200 image called canvas.
b. What do you expect (stamp-htrans my-stamp -2) to do?
c. Check your answer experimentally.
d. What do you expect the following code to do?
> (image-render-stamp! canvas (stamp-htrans my-stamp -2))
e. Check your answer experimentally.
f. As you should have discovered, trying to draw the left-translated
figure will result in an error, since some of the pixels are outside
the boundary of the image. Fix this problem by rewriting
image-render-spot! so that if the spot is to be drawn outside
of the bounds of the image, nothing happens. (That is, a spot is
not drawn and an error message is not produced.)
g. Verify that your revised procedure now lets us draw the portion
of the left-translated figure that is still on screen.
Exercise 5: Replicating Figures
a. What do you expect the value of aardvark to be after the following definition?
> (define aardvark (map (lambda (offset) (stamp-htrans my-stamp offset)) (list 1 2 4 8)))
b. Check your answer experimentally.
c. As you should have discovered, these instructions create a list of lists of spots.
d. What effect do you expect the following command to have?
> (image-render-stamp! canvas aardvark)
e. Check your answer experimentally.
f. What effect do you expect the following command to have?
> (for-each (lambda (stamp) (image-render-stamp! canvas stamp)) aardvark)
g. Check your answer experimentally.
h. What effect do you expect the following command to have?
> (for-each (lambda (factor) (image-scaled-render-stamp! canvas my-stamp factor))
(list 1 2 3 4 5))
i. Check our answer experimentally.
Explorations
Explorations are intended for students interested in further
exploring the design aspects of these techniques. They also provide
students who finish early with extra activities that may challenge
them in different ways.
In this lab, you've discovered how to write concise code that lets
you render the same list of spots in multiple scales and with multiple
offsets.
Create a simple stamp, and, from that stamp, create
an interesting image by both offsetting and scaling the stamp.
Some Useful Definitions
This lab depends on a variety of procedures that you should have defined
in the lab on representing
stamps as lists of spots. However, some of you may have not
had time to complete all of those problems and others of you may not
be sure of your answers. Grab most of the procedures from that lab, and
the rest from here.