Held: Wednesday, 1 April 2015
Back to Outline 33 - Turtle Graphics.
On to Outline 35 - Geometric Art Through Numeric Recursion.
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
- New Partners!
- Review sessions Thursday at 9:15am (Sam), 1:15pm (Sam), and 8pm (Alex).
- After reflecting, I flipped the topics for today's class and Friday's class.
Sorry!
- Exam 2 returned.
- Strongest arguments:
- "The main goal of the exams is that we learn. We've learned. Exam grades
are likely to be relatively consistent across exams, so not counting this exam
is unlikely to have an effect on students who did not get inappropriate help."
- "Many of us worked very hard on this exam and should see rewards for our
hard work."
- "You should have a policy that does not discourage people from coming forward."
Upcoming Work
Extra Credit Opportunities
Academic
- Scholars Convocation.
- CS Table Friday: Therac 25 (readings coming soon)
Peer Support (Morning Section)
- Julia's radio show, "The Hot Box". Wednesday night/Thursday morning
1:00-2:00 a.m.
Peer Support (Afternoon Section)
- 7:30 p.m. on April 2: Gender as a Process. EE talks afterwards
about using mathy things to understand gender.
Miscellaneous
- Tonight's "Moving Forward" discussion.
Other Good Things (no extra credit)
- 7:30 p.m. on April 2: Gender as a Process. EE talks afterwards
about using mathy things to understand gender.
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.