Class 34: Iteration
Held: Tuesday, 4 April 2017
We consider a technique for stepping through the values in a list, and doing some action (e.g., moving a turtle or drawing something with the GIMP tools) for each value in the list.
Preliminaries
Overview
- Reflections on procedures and side effects
- A useful tool for repetition:
for-each - Comparing and contrasting
map,for-each,repeat, and recursion
Related Pages
Updates
News / Etc.
- Continue partners!
- I am working on fixing the brush size problem.
Rotating reminders
- Visit me in my office! I’m always happy to see students during my office hours. I prefer that you book me at https://rebelsky.youcanbook.me, but you can also just show up during office hours and hope that I’m not busy.
Upcoming Work
- Lab writeup: TBD.
- Homework 7 to be distributed Wednesday.
- Exam 3 distributed a week later
- Project distributed a week later
- Exam 4 distributed two weeks later
- Reading for Wednesday: Geometric Art
Extra credit (Academic/Artistic)
- Today at 4:15 p.m. JRC 101: The 1st Mando lecture. “Reflections By Pulitzer-Prize-winning writer Dale Maharidge & photographer Michael Williamson.”
- Today at noon: CS Table: Gadfly (apps to supportdemocracy)
- BAX Opening Reception (plus Robert Hodierne photographs) Friday 4:30-5:30
Extra credit (Peer)
- Baseball games Saturday at 1pm and 3:30 pm vs. Monmouth (if it ever stops raining)
- Track and Field Saturday at Cornell at 11 am.
Extra credit (Misc)
None right now.
Good things to do
- Women’s Tennis Saturday at 10:00 am
Types of procedures and evaluation order
- This is a short chance to reflect on what we’ve been doing with some aspects of Scheme.
- The first few procedures we learned, like
squareor*or evendrawing-vscalehad a few important properties:- They left their arguments unchanged
- Even if applied to the same argument, it doesn’t matter which order we do the computation (other than inside-out)
- With both the gimp tools and turtles procedures, procedures are
a bit different.
- They are likely to change their arguments. For example, if we move a turtle forward, it is now at a new location.
- Because operations change state, the order in which the operations are applied to the same object does make a difference.
Repetition and Map
mapallows us to repeat operations.- However, you don’t know the order in which the operations are applied. (On some machines, they are even done in parallel. Certainly, when Amazon and Google use their map-reduce technology, they want things done in parallel.)
- So it doesn’t make sense to use
mapwith side-effecting operations, unless you’re feeling lucky. - What’s the alternative? We can use
for-each. It’s almost exactly likemap, except that- Order is guaranteed
- You’re not building a list
- What if you want to build a list and guarantee the order? Some
implementation of Scheme include a variant of
mapcalledmapcar. And you’ll soon learn how to implement that yourself.
Four Mechanisms for Repetition
We’ve now seen four mechanisms for repetition: map, recursion, repeat,
and for-each. When do you use each approach?
map: Goal is to transform each element in a list, and you want the list of transformed values as a result.for-each: Goal is to do a side-effecting-operation for each element in a list, in order, with a focus on accumlating side effects, rather than on computing a result.repeat: Goal is to do exactly the same side-effecting operation a fixed number of times., with an emphasis on accumulating side effects, rather than on computing a result.- Recursion: The most general: Intended to do any kind of repetition, either pure or side-effecting operations, any kind of list.
Another approach to this material
This is the approach I used to use for this material. I’ve kept it in the eboard for historical reasons (and because I may want to go back).
The Problem: Making Spirals
- We ended the turtles lab trying to make spirals.
- Basically, you need to move forward a little and turn a little
- Three possible strategies:
- Move the same amount each time and turn the same amount each time
- Move a little more each time, and turn the same amount each time
- Move the same amount each time, and turn a different amount each time
- The first strategy procedures a circle, and is possible to do with
repeat. - The other two strategies seem difficult to do without a lot of hand coding.
The for-each Function
- We’d like to use
map, but there are two problems.- Officially, you don’t know the order in which the function is
applied when you do a
map; it could be first to last, it could be last to first; it could be all at the same time. - We get back a list, which we really don’t need.
- Officially, you don’t know the order in which the function is
applied when you do a
- Scheme provides
for-eachfor situations like this. - Like
map, but for sequences of actions.for-eachis guaranteed to work in order.for-eachdoesn’t return anything.
Lab
- Do the lab.
- Be prepared to reflect.