Functional Problem Solving (CSC 151 2016S) : EBoards

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


Overview

Quiz Topics and Structure

Topics

Form

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

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.