Held: Monday, 3 November 2014
Back to Outline 33 - Turtle Graphics.
On to Outline 35 - Pairs and Pair Structures.
Summary
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.
Related Pages
Overview
- Reflecting on procedures and side-effects.
- A useful tool for repetition:
for-each
.
- Contrasting
map
, for-each
, repeat
, and recursion.
Administrivia
- Continue partners.
- I hope you had a wonderful weekend and found ways to enjoy the sun.
- Extended/modified office hours continue this week. (More variation than
normal; sorry.)
- Welcome to any prospective students!
- It's preregistration time. Let me know if you have questions about
our other courses. (Email or in-person visits will be fine.)
- Some students in a review session said (approximately): "No one reads
the eboards" (presumably, except during class). Double-blind vote (Evan
counts, you and I close our eyes):
- Do you find eboards useful?
- Should I keep doing eboards?
- Quiz 8 will be returned during lab. We will go over it first.
Upcoming Work
Cool Upcoming Events on Campus
- Peace studies talk Monday at 4:15 p.m. in JRC 101
"With All the World’s Violence, Where is Peace?"
Extra Credit Opportunities
Academic
- CS Extra Thursday: Study-Abroad Opportunities with a CS Component
4:30 p.m., Science 3821.
- CS Table Friday: Open Data.
12:10-12:50, Day PDR.
Peer Support
- Karan's radio show 11pm Thursday nights on KDIC
- Evan's radio show 5pm Friday nights on KDIC
- Donna's radio show Sunday midnight on KDIC
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
square
or *
or
even drawing-vscale
had 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
map
allows 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
map
with side-effecting operations,
unless you're feeling lucky.
- What's the alternative? We can use
for-each
. It's almost exactly
like map
, 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
map
called
mapcar
. 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.
- Scheme provides
for-each
for situations like this.
- Like
map
, but for sequences of actions.
for-each
is guaranteed to work in order.
for-each
doesn't return anything.
Lab
- Do the lab.
- Be prepared to reflect.