Skip to main content

Laboratory: Iteration

Summary: In this laboratory, you will experiment with the for-each procedure, which we use for iterating over lists.

Preparation

a. Make a copy of iteration-lab.rkt, which contains some useful code for this lab.

Exercises

Exercise 1: Improving Our Spirals

As you saw in the self-checks for the reading, you can make a spiral using turtle-action-01! on a list of values of increasing size.

> (define world1 (image-show (image-new 200 200)))
> (define turtle1 (turtle-new world1))
> (turtle-teleport! turtle1 100 100)
> (for-each (l-s turtle-action-01! turtle1)
            (list-drop (iota ___) 1))

a. turtle-action-01! procedure uses an angle of 23 degrees. What do you expect to have happen if you use a smaller or larger angle?

b. Check your answer experimentally, using a variety of values, some smaller than and some larger than 23.

c. One disadvantage of the spiral made by turtle-action-01! is that the spiral doesn’t seem very smooth because you can see the line segments. Spend a minute or two discussing how you might rewrite the call to turtle-action-01! to make the spiral a bit smoother, if not exactly the same. (You don’t have to change the call; just think about what you might do.)

Exercise 2: A Procedure for Making Spirals

The turtle-spiral! procedure makes an “inward spiral” of a particular number of steps by using for-each and turtle-action-02!.

Check how the procedure works by making spirals of with 10 steps, 20 steps, 30 steps, 50 steps, and 80 steps.

Exercise 3: Repeating Spirals

You’ve seen two mechanisms for repeating the turtle actions (and, presumably, other procedures with side effects): repeat and for-each. We’ve used for-each to build turtle-spiral!. Let’s see what happens if we repeat that procedure.

a. What do you expect to happen if you use repeat to have a turtle draw 5 spirals, each of with 80 steps, as in the following?

> (define world4 (image-show (image-new 400 400)))
> (define tommy4 (turtle-new world4))
> (turtle-teleport! tommy4 200 200)
> (repeat 5 (lambda () (turtle-spiral! tommy4 80)))

b. Check your answer experimentally.

c. What do you expect to happen if you use 85 or 75 steps in the spiral?

d. Check your answer experimentally. Then suggest what is likely to happen with longer spirals.

Exercise 4: Repeating Spirals, Revisited

You’ve seen that we can use for-each to repeat actions with different parameters. Why not repeat procedures developed using for-each? Let’s see what happens if we draw a sequence of spirals, each with a different number of steps.

a. What image do you expect the following to produce?

> (define world5 (image-show (image-new 400 400)))
> (define tommy5 (turtle-new world5))
> (turtle-teleport! tommy5 200 200)
> (for-each (lambda (i) (turtle-spiral! tommy5 (+ i 10)))
            (iota 10))

You may find it easier to think about that last command as a composition.

> (for-each (o (l-s turtle-spiral! tommy) (l-s + 10))
            (iota 10))

b. Check your answer experimentally.

c. What do you expect to happen if you use larger numbers of steps, such as with (l-s + 30)?

d. Check your answer experimentally.

e. What do you expect to happen if we make the list of numbers of steps longer, such as with (iota 30)? In this case, assume we still use (l-s + 10), as in (map (l-s + 10) (iota 30)).

Exercise 5: Iteration with GIMP Tools

As the reading suggests, for-each can take multiple lists as parameters, in which case it takes the corresponding element from each list as a parameter.

We can use this technique to draw complex shapes with the GIMP tools procedures. For example, we can use for-each with image-select-ellipse! to select multiple regions and then fill them.

> (define world6 (image-show (image-new 200 200)))
> (image-select-nothing! world6)
> (for-each (lambda (i)
              (image-select-ellipse! world6 ADD (* 10 i) (* 9 i) 12 10))
            (iota 20))
> (image-fill! world6)
> (image-select-nothing! world6)

a. What image do you expect this code to produce?

b. Check your answer experimentally.

c. Suppose we use image-stroke-selection! instead of image-fill!. What image do you expect the modified code to produce?

d. Check your answer experimentally.

e. Extend the expression above to vary the width or height of the ellipses.

For Those With Extra Time

Those who find that they have finished the main problems in this laboratory may find it useful to work on the extra problems (which emphasize coding) or the explorations (which emphasize images).

Extra 1: Working with Multiple Turtles

As you may recall from the reading, we can also use for-each to work with a collections of turtles.

a. The reading claims that we can make a list of turtles in different positions with the following code. Check that assertion by using for-each and turtle-forward! to move each of the turtles forward ten units.

> (define world7 (image-show (image-new 200 200)))
> (define turtles (map turtle-new (make-list 20 world7)))
> (for-each turtle-teleport!
            turtles
            (map (l-s * 10) (iota 20))
            (map (l-s * 5) (iota 20)))

b. Write or find a procedure that takes one parameter, a turtle, and makes the turtle do something simple. (E.g., you might have the turtle move forward ten steps, turn right 36 degrees, move forward five steps, and turn left 6 degrees.

c. Use for-each to apply your procedure to each of the turtles in the list.

Extra 2: Working with Multiple Turtles, Revisited

a. Create another list of twenty turtles, as in the previous exercise.

b. Suppose we use the following instruction before we tell our turtles to move or draw. What effect do you expect the instruction to have?

> (for-each turtle-turn! turtles (map (l-s * 18) (iota 20)))

c. Check your answer by using for-each to apply your action from the previous exercise to each turtle.

Explorations

In exercises 4 and 5, you found that different ways of repeating the turtle-spiral! procedure gave you very different images. Here are three extensions of those images.

Exploration 1: A Spiral of Spirals

(Explorations are usually open-ended. However, some students prefer a particular goal. This exploration provides such a goal. Those who wish a more open-ended exploration should go on to one of the next two explorations.)

Write instructions that create a “spiral of spirals”. That is, your instructions should create a large spiral which is, in essence, a sequence of smaller spirals.

Exploration 2: Exploring Combinations of Spirals

Create an image you find interesting (appealing, puzzling, “organic”) using a combination of for-each and repeat with turtle-spiral!.

Exploration 3: Different Base Spirals

All of these images are based on the inward spiral created by repeating turtle-action-02!.

a. Replace turtle-action-02! in the body of turtle-spiral! and determine its effect.

b. Create an image you find interesting (appealing, puzzling, “organic”) using a combination of for-each and repeat with your new version of turtle-spiral!.