Held: Monday, 2 November 2015
Back to Outline 33 - Turtle Graphics.
On to Outline 35 - Pause for Breath.
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.
- Quiz 8 returned; new grades distributed Sunday night.
- No, you should not be up as late as I was.
- Exam 2 answer key forthcoming asap. (Still some delays due to a variety
of issues.)
- Review sessions this week:
- Monday, 8pm ()
- Wednesday, 8pm ()
- Thursday, 10am (SamR)
- Thursday, 8pm ()
Upcoming Work
- Reading for Tuesday:
Geometric Art.
- HW 6 due Tuesday night.
- Exam 3 to be distributed on Tuesday.
- Today's Lab writeup (due before class Wednesday)
Extra Credit Opportunities
- If possible, send me these in advance.
Academic
- Any visit to the current show in the Faulconer gallery.
- Learning Deconstructed Workshop Series, Tuesdays at 11 in JRC 209.
- CS Table Tuesday: Why people support and bully online.
- Sexual Misconduct at Grinnell College: Results from the 2013 and 2015
Sexual Conduct Surveys. 1-3 pm, November 8, JRC 101.
- CS Extras Thursday: Bluetooth, Robots, and CS 161.
Peer Support
Regular Peer Support
- Pals of PALS, pals@grinnell.edu, normally Saturday at 7:45 am (breakfast
included), Sunday at 4:45, and Mondays at 4:45. Requires sign up in
advance.
- Socrates Cafe', Saturdays, Younker, 2pm.
- Pun Club, Saturdays, 4pm, Way over Younker.
Upcoming Peer Support
- November 14, Orchestra Concert, Sebring Lewis, 7:30 p.m.
- Women's Basketball Opening Game, 1pm, Nov. 15, vs. Silver Lake.
- One-act Festival Dec. 5 & 6.
Other Good Stuff to Do
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.