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.scm, which contains some useful code for this lab.
Exercises
Exercise 1: Experimenting with the Basic Actions a. The reading claims that by applying the turtle-action-01! procedure to the numbers from 1 to n, we can get one form of spiral. Check this claim using something like the following code. > (define world1 (image-show (image-new 200 200))) > (define turtle1 (turtle-new world1)) > (for-each (l-s turtle-action-01! turtle1) (list-drop (iota ___) 1)) b. The reading claims that by applying the turtle-action-02! procedure to the numbers from 1 to n, we can get another form of spiral. Check this claim using similar code to that above. c. Does it matter whether we start with 1 or 0 for the list of distances or angle? Check your answer experimentally.
Exercise 2: Improving Our Spirals a. turtle-action-01! 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. Rewrite the instructions to make the spiral a bit smoother.
Exercise 3: A Spiral Procedure The turtle-spiral! 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 length (number of steps) 10, 20, 30, 50, and 80.
Exercise 4: Repeating Spirals You've seen two mechansisms 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 length 80, as in the following? > (define world4 (image-show (image-new 400 400))) > (define tommy4 (turtle-new world4)) > (turtle-teleport! tommy4 200 200) > (repeat 5 (r-s turtle-spiral! 80) tommy4) b. Check your answer experimentally. c. What do you expect to happen if you use 85 or 75 for the length of the spiral? d. Check your answer experimentally.
Exercise 5: 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 of different lengths. 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 (l-s turtle-spiral! tommy5) (map (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 lengths longer, such as with (iota 30)? (In this case, assume we still use (l-s + 10).
Exercise 6: 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 (left top) (image-select-ellipse! world6 ADD left top 12 10)) (map (l-s * 10) (iota 20)) (map (l-s * 9) (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! 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!.