CSC 151: Extra Session 5 (Thursday, 2 Oct. 2014)
================================================

_What is going to be on the quiz tomorrow?_

* Lists (using `list`, `make-list`, and `iota`)
* `map`
* More simple lambda expressions
* Conditionals
* GIMP procedures (e.g., `image-select-rectangle!`)
* `compose`, `o`, `l-s`, `r-s` 

_Left section and right section are the same if the function is
commutative, right?_

> Yeah.

_Do we need to know how to select a region of an image?_

> Yes.

_Do we need to know the form of the parameters?_

> That's why you get to bring notes.  But I'll accept approximations when
  you write.  If you read, I'll expect that you mostly know what procedures
  do.

_You mentioned `(image-select-ellipse! image SUBTRACT ....)`.  What does
that do?_

> See the DrRacket page.

    > (define pear (image-show (image-new 400 400)))
    > (image-select-rectangle! pear REPLACE 100 100 200 250)
    1
    > (image-select-rectangle! pear ADD 200 200 200 100)
    1
    > (image-select-rectangle! pear REPLACE 100 100 200 250)
    1
    > (image-stroke! pear)
    '()
    > (image-select-rectangle! pear REPLACE
                               200 200 200 100)
    1
    > (image-select-rectangle! pear REPLACE 100 100 200 250)
    1
    > (image-select-rectangle! pear ADD 200 200 200 100)
    1
    > (image-select-rectangle! pear SUBTRACT
                               200 200 200 100)
    1
    > (context-set-fgcolor! "red")
    '()
    > (image-stroke! pear)
    '()
    > (image-select-rectangle! pear REPLACE 100 100 200 250)
    1
    > (image-select-rectangle! pear INTERSECT
                               200 200 200 100)
    1
    > (context-get-brush)
    "2. Hardness 100 (editable)"
    > (context-set-brush! "2. Hardness 100" 20)
    '#(brush
       "2. Hardness 100 (editable)"
       circle
       10.0
       2
       1.0
       1.0
       0.0
       10)
    > (context-set-fgcolor! "blue")
    '()
    > (image-stroke! pear)
    '()
    > (context-update-displays!)> (define pear (image-show (image-new 400 400)))
    > (image-select-rectangle! pear REPLACE 100 100 200 250)
    1
    > (image-select-rectangle! pear ADD 200 200 200 100)
    1
    > (image-select-rectangle! pear REPLACE 100 100 200 250)
    1
    > (image-stroke! pear)
    '()
    > (image-select-rectangle! pear REPLACE
                               200 200 200 100)
    1
    > (image-select-rectangle! pear REPLACE 100 100 200 250)
    1
    > (image-select-rectangle! pear ADD 200 200 200 100)
    1
    > (image-select-rectangle! pear SUBTRACT
                               200 200 200 100)
    1
    > (context-set-fgcolor! "red")
    '()
    > (image-stroke! pear)
    '()
    > (image-select-rectangle! pear REPLACE 100 100 200 250)
    1
    > (image-select-rectangle! pear INTERSECT
                               200 200 200 100)
    1
    > (context-get-brush)
    "2. Hardness 100 (editable)"
    > (context-set-brush! "2. Hardness 100" 20)
    '#(brush
       "2. Hardness 100 (editable)"
       circle
       10.0
       2
       1.0
       1.0
       0.0
       10)
    > (context-set-fgcolor! "blue")
    '()
    > (image-stroke! pear)
    '()
    > (context-update-displays!)

__Can you explain `image-select-all!`, `image-select-nothing!`, and
`context-update-displays!`?_

> Gimp doesn't always show the results of a command. `context-update-displays!  forces Gimp to update the displayed image (in most cases).

> In lab, you learned that when you have something selected and do 
  `image-draw-line!`, the line is only drawn through the selected part.
  So, we should either unselect or select all.

> Which is better?  It depends on what you want to do.  If you want to make
  simulated swiss cheese, selct-all followed by image-select-ellipse! SUBTRACT
  is probably better.  Sam uses `image-select-nothing!` more frequently.

_Why didn't you make image-stroke update the displays?_

> Laziness.  It also slows down the program.

_Why do the select operations have a `!`?

> Because we're changing the selection.  (Although not the image.)  Laziness.

_What does `image-select-all!` do?_

> Selects the entire canvas.

_Why do I get a faded version of a color when I use `image-stroke!`?_

> `image-stroke!` uses the currently selected tool, which is the airbrush.

Sample Quiz Questions
---------------------

_Can you give us some sample quiz questions?_

> Write a procedure, `(brighntess->name value)` that converts a brightness 
  value (between 0 and 1) to a name by returning "very-dark" if the
  brightness is less than 0.25, "dark" if it's between 0.25 and 0.5,
  "light" ....

  For the bounds (0.25, 0.5, and 0.7), you can choose the name on either
  side.

        (define brightness->name
          (lambda (value)
            (cond
              [(< val 0.25) "very-dark"]
              [(< val 0.5) "dark"]
              [(< val 0.75) "light"]
              [else "very-light"])))

> We're doing lots of tests.  Use a conditional.

> Match the definitions, using some of the simplifications we talked about
  in class on Wednesday.

_Could we have done `(<= val 1)` as the last test?_

> Yes.

_Could we have used lots of `if` statements instead of a `cond`?_

> Yes.  Probably equally efficient.  But likely to be harder to read.
  (And could be even more efficient.)

_Could you write this using just `and` and `or`?_

        (or (and (< val 0.25) "very-dark")
            (and (< val 0.5) "dark")
            (and (< val 0.75) "light")
            "very-light")
   
Write a procedure, `(irgb-reds colors)` that takes as a parameter a list
of colors and returns a list of colors with equivalent red components but
green and blue components set to 0.  For example, if the input list has
colors "100/25/255", "20/200/200", and "0/0/0", the output list should have
the colors "100/0/0", "20/0/0", and "0/0/0".

> We probably have to turn the colors into irgb-colors.  (Or Sam should have
  been clearer that they are already irgb-colors.)  Since we have a list,
  we need to use map.

> Now we have a list of irgb-colors.  We want to do something to each.  
  That means we need to use `map` again.

> I don't know a procedure that removes the red and green components, so
  I'll need to write one.

> Sam doesn't like names, so I'll make it anonymous.

        (define irgb-reds
          (lambda (colors)
              (map (lambda (color) (irgb (irgb-red color) 0 0))
                   (map color->irgb colors))))
       
What is the value of `(append (reverse (iota 3)) (map increment (iota 3)))?`

        '(2 1 0 1 2 3)

> Append says that you just join the two lists together.

> `(iota 3)` is `(0 1 2)`.  Reversed is `(2 1 0)`.

> `(iota 3)` is `(0 1 2)`.  `increment` adds 1.  When you map it, you
  add 1 to each, giving `(1 2 3)`.

