Skip to main content

CSC 151.01, Class 34: Iteration

Overview

  • Preliminaries
    • Notes and news
    • Upcoming work
    • Extra credit
    • Questions
  • Reflections on procedures and side effects
  • Comparing and contrasting map, for-each, repeat, and recursion
  • Lab

News / Etc.

  • Continue partners!
  • I plan to talk a bit more today than yesterday. Sorry.
  • I am working on fixing the brush size problem.
  • Prospective!
    • Welcome!
    • Come to Grinnell because we have great opportunities
    • Plus your brother is lonely without seeing you regularly
    • Dairy barn ice cream is better than dhall. But the former gas station (“Candyland”) is even better. Plus they have a board game you played in childhood.

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 support democracy)
  • 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

Questions

Reflections on procedures and side effects

  • Two different models of procedures.
  • Originally: A procedure computes a value. square computes a value by multiplying a number by itself.
    • (square 5) => 25
    • (square 5) => 25
    • (define x 11)
    • (square 5) => 25
  • Now, procedures can change the state of the system. You often care more about that change in state than the return value, but sometimes both.
    • (turtle-forward! yertle 10) - yertle has moved forward ten units. We get back a turtle, which looks like #<procedure:...> If the brush is down, we see short line.
    • (turtle-forward! yertle 10) - I get back #<procedure...>, we get back the same turtle, but it’s in a different position. Something different appears on the screen; our state is different.
    • (turtle-x yertle) - [returns a shoot (it is spring, after all)] returns the x position of the turtle.
    • Suppose I call (turtle-x yertle) and get 25. I then do some more work, and call (turtle-x yertle) again. Will I get
      1. It depends. on whether or not I’ve moved the turtle.
  • Changing the state can lead to confusing situations.

Comparing and contrasting map, for-each, repeat, and recursion

  • map - maps a function onto a list (calling the function on each element of the said list), returns a list of the results of the function calls.
  • for-each - calls the function on each element of the list, but returns nothing.
    • (for-each square (list 1 2 3)) -> computes 1 4 9 but doesn’t let you see anything. You should probably use map.
    • (for-each (l-s turtle-action01! yertle) (list 5 1 23 1)) does the action with four different inputs. We don’t really need a list of turtles back, so we don’t use map.
    • Used to repeatedly call a procedure that produces a side effect
  • repeat - Repeatedly call a procedure that produces a side effect.
    • (repeat 5 square 10) - pointless
    • repeat always gives the procedure the same parameter; for-each gives it different parameters (potentially)
  • recursion
    • Works with everything; most general case. (Tends to be longer.)

Lab

Note: Here’s the fix for the brush size issue

  1. Quit DrRacket

  2. Open a terminal window

  3. Type /home/rebelsky/bin/csc151-update

  4. Start DrRacket

  5. Try changing the turtle’s brush size!