CSC151.02 2016S, Review Session 2 (2016-02-04)
==============================================

* Thank you to the many people who showed up, so that we were able to
  have a review session.

_Overview_

* Quiz topics and structure.
* Your questions.
* Sample quiz questions.
* More questions.

Quiz Topics and Structure
-------------------------

Topics

* Scheme's order of evaluation and sequencing.
* Definitions (in Scheme).
* Arithmetic operations, including things like `max`, `min`,
  `floor`, `ceiling`, `round`, `quotient`, and `remainder`.
    * You do NOT have to understand the difference between `remainder`
      and `modulo`.
* RGB colors, and the operations on integer-encoded RGB colors.
* Basic RGB transforms (although we will remind you of what they do):
  `irgb-redder`, `irgb-greener`, `irgb-bluer`, `irgb-darker`,
  `irgb-lighter`, `irgb-complement`, `irgb-rotate`, `irgb->string`
* Function composition.  E.g., `(compose irgb-redder irgb-darker)`.
* NOT sectioning.
* NOT `image-load`, `image-save`, `image-variant`

Form

* "evaluate this expression (or series of expressions)"
* "using composition, write a procedure to compute ..."
* "given this definition, what does this procedure do?"

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

_I can't do math in my head.  Are calculators okay?_

> Probably not; I don't think we have anything particularly complicated
  on the quiz.  Mostly addition and subtraction.  Plus, you have paper!

_Do we have to memorize how much each transform changes things?_

> No.  We'll tell you.  We may be cruel enough that we will give
  you different definitions (e.g., "assume `irgb-redder` adds 11 to the
  red component, bounding at 255 ...")

_Do we have to be able to convert RGB triples to those unreadable numbers?_

> NO!

_What transformations can't be undone?  It seems like most of the time
 we are adding and subtracting, so you just do the inverse._

> You can't do the inverse when we "cap" the value.  E.g.,
  `(irgb-lighter (irgb-darker (irgb 0 10 15)))` gives us
  `(irgb 16 16 16)`, which is not the same.  If the result of
  `(irgb-darker x)` is black, we can't tell which of the 8000 or so
  fairly dark colors `x` was.

> Similar issues for `irgb-redder`, `irgb-bluer`, `irgb-greener`,
  `irgb-lighter`.

_Is there a way to add 3 to the red component, assuming `color` is already
an integer-encoded IRGB color?_

> `(irgb-add color (irgb 3 0 0))`

_Do we get extra credit for catching typos in your quizzes?_

> Not usually.  Sometimes typos affect solvability, in which case yes.

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

Given that

* `irgb-redder` adds 32 to the red component (bounding at 255)
* `irgb-greener` adds 32 to the green component (bounding at 255)
* `irgb-bluer` adds 32 to the blue component (bounding at 255)
* `irgb-complement` subtracts each component from 255
* `irgb-darker` subtracts 16 from each component (bounding at 0)
* `irgb-lighter` adds 16 to each component (bounding at 255)

### Evaluate These Expressions (Show Your Work)

#### Colors

    (irgb->string (irgb-darker (irgb-lighter (irgb 16 192 240))))

    (irgb->string (irgb-lighter (irgb-darker (irgb 16 192 240))))

##### A Solution

    (irgb-lighter (irgb 16 192 240)) -> (irgb 32 208 255)
    (irgb-darker (irgb 32 208 255)) -> (irgb 16 192 239)
    "16/192/239"

    (irgb-darker (irgb 16 192 240)) -> (irgb 0 176 224)
    (irgb-lighter (irgb 0 176 224)) -> (irgb 16 192 240)
    "16/192/240"

#### Floors and Ceilings

    (floor (+ 1/2 (ceiling (- 1/2 (round 2.5)))))

    (ceiling (+ 1/2 (truncate (- 1/2 (floor 2.5)))))

##### Solutions

    (round 2.5) -> 2
    (- 1/2 2) -> -3/2
    (ceiling -3/2) -> -1
    (+ 1/2 -1) -> -1/2
    (floor -1/2) = -1

    (floor 2.5) -> 2
    (- 1/2 2) -> -3/2
    (truncate -3/2) -> -1
    (+ 1/2 -1) -> -1/2
    (ceiling -1/2) -> 0

### Write a Procedure 

#### Much Darker

Without using `section`, `lambda`, or `irgb-darker`, write a procedure,
`irgb-much-darker`, that takes an integer-encoded RGB color as input
and subtracts 32 from each component (bounding at 0).

##### A Solution

    (define irgb-much-darker (irgb-complement irgb-lighter irgb-lighter irgb-complement))

For a component, `c`

    255 - (16 + (16 + (255 - c)))
    255 - (16 + 16 + 255 - c)
    255 - (16 + 16 + 255) + c
    255 - 32 - 255 + c
    -32 + c

If we were allowed to use `section`

    (define irgb-much-darker (section irgb-subtract <> (irgb 32 32 32)))

#### Less Blue

Without using `section`, or `lambda` write a procedure, `irgb-less-blue`,
that takes an integer-encoded RGB color as input and subtracts 32 from
the blue component (bounding at 0).

##### A Solution

### Quad

Without using `section` or `lambda`, write a procedure, `quad`, that
takes a number is input and computes the number to the fourth power.

#### A Solution

    (define quad (compose square square))

### Interpret a Procedure or Expression

#### A Color Transformation

Consider this definition

    (define fun (compose irgb-darker irgb-redder irgb-greener))

What is

    (irgb->string (fun (irgb 16 192 210)))

Explain *concisely* what `fun` computes.

#### Another Color Transformation

Consider this definition

    (define fun (compose irgb-complement irgb-darker irgb-bluer irgb-greener irgb-complement))

What is

    (irgb->string (fun (irgb 16 192 210)))

Explain *concisely* what `fun` computes.

> `fun` subtracts 16 from the green and blue components and adds 16 to
  the red component.

> _No, I probably wouldn't ask something quite that complex on a quiz,
  but maybe on an exam._

#### Fun with Numbers

Assume that `(square x)` computes `(* x x)`.  Consider this definition

    (define fun (compose floor square))

What is

    (fun 3.9)

#### A Strange Expression

Explain what the result of the following expression is in terms of `x`.

    (- x (remainder x 5))

##### A Solution

> The difference between x and the remainder of x/5.  (No; just a translation
  into English.)

> The nearest multiple of 5 to x.

#### Potential Values

What values can the following expression take on?

    (- (floor x) (round x))

More of Your Questions
----------------------

_What does `truncate` do?_

> Cuts off everything after the whole part.

> Rounds down for positive, rounds up for negative.

_What does "explain concisely" mean?_

> Talk about the apparent purpose, rather than the steps involved.

> Give a "high level" overview.
