Held: Wednesday, 6 April 2016
Back to Outline 34 - Turtle Graphics.
On to Outline 36 - Randomized (Unpredictable) Drawing.
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.
- Happy Birthday anonymous student!
- Thank you for sharing the food stuffs that are not nearly as good
as those your family would make.
- Good luck to our improvisational students as the travel off to an
another "three vowels plus a potentially vowel-like letter" state.
- We have lots of introductory stuff to do. I'll still try to keep it
brief.
- Some of you have been asking about CSC 161 and CSC 207. We are committed
to ensuring that every prospective major can take CSC 161 and CSC 207 in
the semester in which they want to take those courses. We will do our best
to ensure that every student who wants to take CSC 161 and CSC 207 can
do so at some point.
- Exam stuff
- I encourage you to try a variant of "You must try, and then you must
ask" https://blogs.akamai.com/2013/10/you-must-try-and-then-you-must-ask.html. (Ten minutes of the try section suffices.)
- Yes, I'll deal with the "I have no idea how to ask a questin, but I
need help" questions. However, you do need to send me code and notes.
- The prologue will not be available until tonight.
Reminders
- Office hours this week
- See http://rebelsky.youcanbook.me.
- Ask me about other available times.
- Tutor hours
- Sunday, 3-5 p.m.
- Sunday-Thursday, 7-10 p.m.
- Weekly review sessions:
- Wednesday at 8pm in the CS Commons with Evan.
- Thursday 10 am review session with Sam.
- Thursday at 8pm in the CS Commons with Alex.
Upcoming Work:
- Reading for Friday:
- Lab Writeup:
- Send email titled CSC 151 Lab Writeup 35 (Your Names)
- Do not include the underscores.
- Send to CSC151-02-grader@grinnell.edu
- Due before class on Monday.
- Exam 3 distributed.
- Quiz on Friday.
- Numeric recursion.
- Letrec and named let.
- Turtles.
Extra Credit
- Send your reports to rebelsky@grinnell.edu with subject
"CSC 151 Extra Credit". (Do not include the quotation marks.)
- Send opportunities to me before class with subject
"CSC 151 EXTRA CREDIT OPPORTUNITY!"
Academic / Artistic
- Cafe Ta'amon on April 6 at 6pm, in JRC 101 (I think).
- Cinderella on April 6 at 7pm, somewhere in Bucksbaum (I think).
- CANCELLED! Roxane Gay Convocation on Thursday (11 a.m., JRC 101).
- BAX Juror Jane Gilmor talk, noon, Thursdya, BCA 152. Pizza!
- BAX Friday, 4pm, Bucksbaum.
Peer
- Post-break ExCo on British Politics TONIGHT (and subsequent Wednesdays)
at 8:00 in JRC 203. Just show up; you don't need to sign up.
- Fun Japanese Spring Festival Friday at 7pm. Origami and food!
Miscellaneous
- Pioneer weekend.
- Scarlet and Give Back Day Thursday (I will fund your donation of $5 if
you find that to be an economic burden)
- Host a prospective student.
Regular Peer
- Social Dance Workshop Tuesdays 7:00-8:00 in Bucksbaum Dance Studio
- Pun club Saturdays at 4pm in Younker
- Electronic Potpourri on KDIC Fridays at Five
- Space Odyssey KDIC Fridays at Six
- Bollywood, Fridays, 7:30-8:30, Younker
- Effective Altruism club, 2:30-3:30 Sundays in JRC 226.
Misc
Far in the Future
- Lords of the Flies, April.
- Adaptation of Rushdie's East West. Early May.
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.