CSC151.02 2015S, Review Session 3: Procedures and Beyond
========================================================

_Overview_

* You ask questions, I attempt to answer.
* I ask questions, you attempt to answer.
* We look at some sample quiz questions.

_Admin_

Your Questions
--------------

_If I know that the left borders of two pictures are the same and
the top borders of the two pictures are the same, can I say that the
"position" of the two pictures is the same?_

> Position is vague.  I often center.  Unless their widths and heights
  are the same, their centers are unlikely to be the same just because
  the top-left is the same.

> I would normally check as much as possible.

        (define similar
          (lambda (drawing-one drawing-two)
            (and (= (drawing-left drawing-one) (drawing-left drawing-two))
                 (= (drawing-top drawing-one) (drawing-top drawing-two))
                 (= (drawing-width drawing-one) (drawing-width drawing-two))
                 (= (drawing-height drawing-one) (drawing-height drawing-two))
                 (equal? (drawing-type drawing-one) (drawing-type drawing-two))
                 (equal? (drawing-color drawing-one) (drawing-color drawing-two)))))

> In terms of unit testing of `drawing-at`

        (define sample-drawing-1
          (drawing-hshift
           70.3
           (drawing-vshift 
            -10
            (drawing-scale 
             123
             (drawing-group
              (recolor-drawing "red" drawing-unit-circle)
              (scale-drawing 0.5 drawing-unit-square))))))
        (define sample-result-1
          (drawing-at sample-drawing-1 10/3 -7/2))
        (test-case 
         "position of center"
         (check-= (drawing-center-x sample-result-1) 10/3 .001)
         (check-= (drawing-center-y sample-result-1) -7/2 .001))
        (test-case
         "size of drawing"
         (check-= (drawing-width sample-result-1)
                  (drawing-width sample-drawing-1)
                  .001)
         (check-= (drawing-height sample-result-1)
                  (drawing-height sample-drawing-1)
                  .001))
        (test-case
         "appearance of drawing"
         (check-equal? (drawing-type sample-result-1)
                       (drawing-type sample-drawing-1))
         (check-equal? (drawing-color sample-result-1)
                       (drawing-color sample-drawing-1)))

_How do we know the full path to an image we download?_

> You should know where it gets downloaded to.  If you save to Downloads
  and it's called Cute_kitten.jpg, you would use
  '/home/username/Downloads/Cute_kitten.jpg`.

_What's the difference between `check-=` and `check-equal?`?_

> In Scheme, many numbers are approximated.  You will find that
  (square (sqrt 2)) is slightly different than 2.  
  `check-equal?` is exact equality.  That can be appropriate, 
  particularly when you're working with strings or symbols or other
  non-numeric values.   For numbers, approximation means you should
  indicate how close is "close enough".  The `check-=` procedure
  allows you to specify what is "close enough".

_When your tests fail, how do you know which ones failed?_

> It's supposed to print the string associated with the test case.

        #lang racket
        (require gigls/unsafe)
        (require rackunit)
        (require rackunit/text-ui)
        
        (define sample-tests
          (test-suite 
           "sample tests"
           (test-case
            "test sqrt 2"
            (check-equal? (square (sqrt 2)) 2))
           (test-case
            "test sqrt 4"
            (check-equal? (square (sqrt 4)) 4))
           (test-case
            "test sqrt 10"
            (check-equal? (square (sqrt 10)) 10))
           (test-case
            "test sqrt 2 reversed"
            (check-equal? (sqrt (square 2)) 2))))
           
        Racket, version 5.3.6 [3m].
        Language: racket; memory limit: 128 MB.
        > (run-tests sample-tests)
        sample tests > test sqrt 2
        test sqrt 2
        FAILURE
        name:       check-equal?
        location:   unsaved-editor2838:11:4
        actual:     2.0000000000000004
        
        expected:   2
        
        . . Check failure
        --------------------
        --------------------
        sample tests > test sqrt 10
        test sqrt 10
        FAILURE
        name:       check-equal?
        location:   unsaved-editor2838:17:4
        actual:     10.000000000000002
        
        expected:   10
        
        . . Check failure
        --------------------
        2 success(es) 2 failure(s) 0 error(s) 4 test(s) run
        2
        > 

_Why doesn't DrRacket let me use the tab key?_

> Tab means "indent to the correct level".  If it's not going where you
  think it should, you probably have the wrong number of parens.

> If your code is really wide, you probably need to put a return before
  the first parameter to a procedure.

_What does it mean that a precondition is "verified" in the six-P
 documentation?_

> Right now, nothing.

> In the futurea, we will have a mechanism for checking whether or not
  a parameter has the right range of values.  We can then choose as
  procedure designers whether or not that is our responsibility.

_What's the difference between single quotation marks and double quotation
 marks?_

> Easy answer: If you want to include a space, you need the double
  quotation marks.

> Harder answer: Scheme treats them differently.  Something with double
  quotation marks can be split apart.  `"Hello There"`.  I am allowed
  to ask for the fifth character or the location of space or the 
  portion that occupies characters 3 through 5 or ..."  I use
  `'rebelsky`, it's "indivisible"

_There seem to be pairs of related procedures, such as `hshift-drawing`
  and `drawing-hshift`.  Why?_

> We originally wrote `(drawing-hshift drawing amt)`.  As you saw in 
  the example from the HW, that's hard to read.  We decided that we
  needed an earier to read alterantive.  Hence, `(hshift-drawing
  amt drawing)`, which we believe is easier to read.  
  Over the long term, you'll find things to write that are even
  more readable.
  `((o (l-s hshift-drawing 23) (l-s vshift-drawing 11)
      (l-s recolor-drawing "red") (l-s scale-drawing 15))
    drawing-unit-circle)

_How should we document our informal experiments?_

> First, you should definitely do those.  Enough that give you
  confidence in correctness.  It depends on the procedure.

> Take your interaction pane.  Copy it.

> Paste it into your definitions pane.  Comment it out by selecting the
  pasted text and using Racket->Comment with Semicolons.

_How many things should we include in the six P's?_

> Enough to give a good idea is probably enough, as long as you don't
  leave out obvious cases.

_Should our `tip` procedure accept negative tips?_

> No.  That would be immoral.

        ;;; Preconditions:
        ;;;   cost is positive
        ;;;   percent is non-negative

> If your procedures preconditions are not met, there are no requirements
  for it to do anything in particular.  It can give a "correct" result.
  It can crash the system.  It can give an incorrect result.  It can
  run forever.

What will the quiz look like?
-----------------------------

Probably two questions, one of which will be "read code and figure it
out" the other will be "write code".

Example "What does this do?"  Give the six P's.

    (define proc
      (lambda (drawing)
        (* 1/2 (+ (drawing-left drawing)
                  (drawing-right drawing)))))

> One answer: "It computes the left side.  It computes the right
  side.  It adds them together.  It multiplies by 1/2."

> Another answer: "It finds the x coordinate of the center."

> I generally like "What goal does this seem to achieve" rather than
  "How does it achieve the goal?"

Example with full 6P's

   (define rect
     (lambda (center-x center-y width height)
       ...))

   ;;; Procedure:
   ;;;   rect
   ;;; Parameters:
   ;;;   center-x, a number
   ;;;   center-y, a real number
   ;;;   width, a real number
   ;;;   height, a positive real number
   ;;; Purpose:
   ;;;   Build a build rectangle of the specified dimensions
   ;;; Preconditions:
   ;;;   center-x must be a real number
   ;;;   width must be a positive number

Example.  "Write this procedure."  Suppose `drawing-right` is not 
defined.  How would you define it based on our other procedures?

        (define drawing-right 
          (lambda (drawing)
            (+ (drawing-left drawing) (drawing-width drawing))))

Example: Drawing
