Functional Problem Solving (CSC 151 2014F) : Labs
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] - [FAQ] [Teaching & Learning] [Grading] [Rubric] - [Calendar]
Current: [Assignment] [EBoard] [Lab] [Outline] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Readings]
Reference: [Setup] [VM] [Errors] - [Functions A-Z] [Functions By Topic] - [Racket] [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [Davis (2013F)] [Rebelsky (2014S)] [Weinman (2014F)]
Misc: [Submit Questions] - [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] - [Issue Tracker (Course)]
Summary: In this laboratory, you will have the opportunity to explore a number of issues relating to predicates, Boolean values, and conditional operations.
You need not do any preparation for this lab, other than the traditional preparation. Make sure you've done the reading and asked questions about it. Start GIMP. Start the MediaScript plugin.
a. Write a predicate, (
, that holds only when the color's
blue component is less than 64.
not-very-blue?
color
)
b. Write a predicate, (
, that
holds only if the red component is greater than the sum of the
green and the blue components.
red-dominates?
color
)
c. Write a predicate, (
, that holds only if no two
components of greyish?
color
)color
differ by more than 8.
As you've noted, the <
procedure can be used
to determine if one number is smaller than another. Can we do similar
comparisons for colors? Certainly. There are, however, a number of
different criteria one could use to compare colors.
a. Write a two-parameter predicate,
(
, that holds only if the green
component of irgb-greener?
color1
color2
)color1
is larger than the green
component of color2
.
b. Write a two-parameter predicate,
(
, that holds
only if irgb-lighter?
color1
color2
)color1
is lighter than
color2
. Note that in doing this comparison,
you should first figure out how light a color is (either by averaging
the three components or by using the more complex lightness computation).
Write a procedure, (
, that determines if the value
named by valid-component?
comp
)comp
is between 0 and 255, inclusive.
and
and or
a. Determine the value and
returns when called
with no parameters.
b. Explain why you think the designers of Scheme had and
return that value.
c. Determine the value and
returns when called
with one, two, and three integers as parameters.
d. Explain why you think the designers of Scheme had
and
return that value.
e. Determine the value or
returns when called
with no parameters.
f. Explain why you think the designers of Scheme had
or
return that value.
g. Determine the value or
returns when called
with only integers as parameters.
h. Explain why you think the designers of Scheme had
or
return that value.
If you are puzzled by some of the answers, you may want to look at the notes on this problem.
You may recall that in problem 2, you explored how certain components can affect whether a color is considered light or dark, and you attempted to find colors that helped you make those determinations. As you know, an important aspect of computer science is taking the informal processes we use and attempting to formalize them. So, let's try that for a similar problem.
Give instructions that someone else could follow in order to
determine the darkest shade of grey that is still considered light.
In writing these instructions, assume that the precise algorithm
irgb-light?
uses is unknown. All you know about
the procedure is that if it considers one shade of grey light, then
it considers every lighter shade of grey light. (Recall that we have
decided that a color is a shade of grey if its three components are
all equal.)
If someone else in the class has also developed a set of instructions, get their instructions and attempt to follow them. (You should also share your instructions with others.)
The reading included a procedure that used and
and
or
to convert colors to black, grey, or red.
;;; Procedure: ;;; irgb-bgw ;;; Parameters: ;;; color, an integer-encoded RGB color ;;; Purpose: ;;; Convert an RGB color to black, grey, or white, depending on ;;; the intensity of the color. ;;; Produces: ;;; bgw, an integer-encoded RGB color ;;; Preconditions: ;;; irgb-light? and irgb-dark? are defined. ;;; Postconditions: ;;; If (irgb-light? color) and not (irgb-dark? color), ;;; then bgw is pure white ("255/255/255") ;;; If (irgb-dark? color) and not (irgb-light? color), ;;; then bgw is pure black ("0/0/0") ;;; If neither (irgb-light? color) nor (irgb-dark? color), then bgw is ;;; grey ("128/128/128") ;;; Problems: ;;; In the unexpected case that none of the above conditions holds, ;;; bgw will be one of black, white, and grey. (define irgb-bgw (lambda (color) (or (and (irgb-light? color) (irgb-new 255 255 255)) (and (irgb-dark? color) (irgb-new 0 0 0)) (irgb-new 128 128 128))))
a. Test this procedure by applying it to some colors you know are light, some colors you know are dark, and some colors you know are neither light nor dark. For example,
>
(irgb->string (irgb-bgw (irgb-new 10 10 10)))
b. Try building an image variant using irgb-bgw
.
For example,
>
(define kitten (image-load "/home/rebelsky/MediaScheme/Images/kitten.png"))
>
(image-show (image-variant kitten irgb-bgw))
c. Write a similar procedure, irgb-bgy
, in which
light colors are converted to yellow, dark colors to blue, and other
colors to green.
d. Try building an image variant using this new version
of irgb-bgy
.
(mostly-red?
color
)
holds if the red component of
color
is the largest of the three components.
(mostly-green?
color
)
holds if the green component
of color
is the largest of the three components.
(mostly-blue?
color
)
holds if the blue component of
color
is the largest of the three components.
a. Write a procedure, (
, that
that converts a color to red if the color is mostly red,
to blue if the color is mostly blue, and to green if the color is
mostly green. If the color is none of those, irgb-dominant
color
)irgb-dominant
should convert the color to a moderate grey. You may want to use the
structure of irgb-bgw
as a pattern for this
procedure.
b. Try applying irgb-dominant
to a variety of colors.
c. Try building an image variant using irgb-dominant
.
(
has value true (and
)#t
)
because “
has a value of
true if none of the parameters have value false”. This
call has no parameters, which means none are false.
and
Alternately, you can think of #t
as the “and-itive
identity”. That is, (and #t
is
x
)x
. When given no parameters,
and
returns its identity value.
(
has value false (or
)#f
)
because “
has value false
if none of the parameters is non-false”. Since this call has
no parameters, none are non-false.
or
Alternately, you can think of #f
as the “or-itive
identity”. That is, (
is or
#f
x
)x
.
When given no parameters, or
returns its
identity value.