CSC151.01 2014F, Class 06: Writing Your Own Procedures
======================================================

_Overview_

* Preliminaries.
    * Admin.
    * Upcoming Work.
    * Extra Credit.
* Sequencing operations in Scheme.
* The drawings-as-values model, revisited.
* Why define your own procedures?
* How to define your own procedures.
* Evaluating expressions in Scheme.
* Lab: Drawings As Values.
* Lab: Procedures (continued tomorrow).

Preliminaries
-------------

### Admin

* Please return your flash drives so that I can supply them to the other
  section of the class.
    * Note: I've given you virtual machines primarily so that you can
      do the short end-of-reading problems and finish labs in the comfort 
      of your room.
    * I strongly recommend that you come to 3813/3815 to work on homework 
      assignments and other problems.
* Quiz 1 returned.  
    * Quizzes have two main purposes: They let you gauge how you well you
      are grasping some material and they let me see how the class is
      doing overall.
    * If you had difficulty on the quiz, you may want to chat with me
      or one of the mentors.  (If you also have difficulty on quiz 2,
      you definitely should talk to me or one of the mentors.)
    * Greatest confusion: `(ceiling -9.3)` -> `-9.0`.
    * The return of quiz 1 serves as my attendance for today.
    * I don't report averages or ranges or anything about grades.
      I recommend that you do not discuss them with each other.
* Apologies for the broken link for part two of today's reading.  
    * It should have been fixed at about 7pm Sunday night.  
    * Please do let me know when you find broken links (and what
      link you are clicking).
* When you run DrRacket in MathLAN, make sure that you are running 5.3.6 
  and not 5.2.1.
* There's a lot of lecture/recitation today.  It seems necessary.

### Upcoming Work

* Lab writeup for the drawings-as-values lab (due Wednesday): Exercise 4, 
  parts a, c, and e.  
  See link on schedule or list of labs for link to submit it.
* I'll assign the writeup for the procedures lab tomorrow (due Friday).
* [HW 2](../assignments/assignment.02.html) is due Tuesday.
    * Clarification: Only one assignment per group.
    * Clarification: No need to submit A.6.
    * Clarification: If we don't specify where code goes, it normally goes 
      in your primary .rkt file (which you copy and paste into the body
      of the email).
    * Note: You may want to carbon copy your colleagues.
    * Please cite.
* Readings for Tuesday: 
    * [How Scheme Evaluates Expressions (Take 2)](../readings/scheme-eval-2.html)
    * [How Pair Programming Really Works](http://www.computer.org/cms/Computer.org/ComputingNow/homepage/2010/0110/W_SW_PairProgramming.pdf) - Distributed in paper form.

### Extra Credit Opportunities

* I'd like to see more of you taking advantage of these opportunities.

#### Academic

* Awesome show at the Faulconer gallery.
* Convo next Wednesday at noon.  "Limiting Armed Drone Proliferation" by 
  Micah Zenko, the Douglas Dillon fellow in the Center for Preventive
  Action at the Council on Foreign Relations and vice chair of the World
  Economic Forum Global Agenda Council on Terrorism.
* Any other event in the Rosenfield Drones program.
* CS Extra, Thursday, September 11: Ajuna Kyaruzi '17 on being a SysAdmin
* CS Table, Friday, September 12: Socially-Asistive Robots Help Children with Autism

#### Peer Support

* Football Games (???)
* Women's Volleyball home matches (???)
* Men's Tennis home matches  (???)
* Women's Tennis (???)
* Anna Christie, Oct. 9-12 (SB plays Marthy)

### Questions

* What should we assume about the reader for part D?
* You can assume that they speak English (or that Google Translate has
  gotten really really good)
* Try to assume little, but it's okay if you choose to assume that they
  are "reasonable" people.
* How should we cite?
    * ; I got help from A.B. Smith on problem D at 3 am.  on 2014-09-04 
    * ; Help from A.B. Smith on lots of things.  Thanks!
    * ; Code taken from http://www.cs.grinnell.edu/~rebelsky/Courses/CSC151/2014F/exams/exam.01.html

Sequencing operations in Scheme
-------------------------------

* "The order in which you do things matters."
* What do we know about ordering in Scheme?
    
        (sqrt (+ (square (- x0 x1)) (square (- y0 y1))))

* This seems to compute the distance between two points.
* What are the operations we need to do: Add (once), subtract (twice), 
  square (twice), square root (once)
* Subtract first because it's the innermost (furthest nested?) operation
    * We tend to sequence inside-out in Scheme
    * The "order of operations" for Scheme
* Which subtraction happens first?
    * "Can't we do them both at the same time?"
    * (- x0 x1) first
    * Does it really matter?  It doesn't matter.
    * "If you have two (or more) arguments to a function, Scheme can 
      choose to evaluate the arguments in either order"
        * We don't know what the policies are, and they may not seem
          consistent to us
    * Scheme might do (- x0 x1) first, then the associated square,
      then the other subtraction, then the other square, then the plus
      then sqrt.
* Are there other ways we can control sequencing (other than inside-out)?
    * Not sure
    * Maybe more lines of code
    * Concrete: Suppose we wanted to be absolutely sure that we
      subtracted y0 from y1 first.

        (define difference-of-ys (- y0 y1))
        (define square-of-difference-of-ys (square difference-of-ys))

    * Another idea (good idea, if "innermost first" is correct, but the
      real policy is "evaluate arguments before applying function")

        (sqrt (+ (square (- x0 x1)) (square (+ 0 (- y0 y1)))))

The drawings-as-values model, revisited
---------------------------------------

What do you expect to get from this?

        (define val 16)
        val
        (sqrt val)
        val

Conclusion: `sqrt` does not change `val`.

What do you expect go get from this?

        (define drawing (scale-drawing 40 drawing-unit-circle))
        (define d2 (recolor-drawing "red" drawing))
        (image-show (drawing->image drawing 100 100))

Discovery: Same thing.  The `recolor-drawing` does not change
`drawing`.

Procedures
----------

* What we called "subroutines" in the reading on algorithms.
* Basic idea:
    * Chunk of code
    * With inputs (we call parameters)
    * Meaning: "When called, substitue for parameters, and evaluate
      the chunk of code."

        (define enlarge-and-recolor
          (lambda (drawing)
            (recolor-drawing "red" (scale-drawing 10 drawing))))

        e.g.,

        (enlarge-and-recolor drawing-unit-circle)

* If there are multiple expressions, we only get the value of the last
  one.  The following will scale, not recolor.

        (define enlarge-and-recolor
          (lambda (drawing)
            (recolor-drawing "green" drawing)
            (scale-drawing 10 drawing)))


Evaluating expressions in Scheme
--------------------------------

Labs
----

