%CustomEntities; %CourseEntities; %CommonEntities; ]>
Laboratory: Boolean Values and Predicate Procedures Summary: In this laboratory, you will have the opportunity to explore a number of issues relating to predicates, Boolean values, and conditional operations.
Preparation 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.
Exercises
Exercise 1: Combining Boolean Values Experience suggests that students understand and and or much better after a little general practice figuring out how they combine values. Fill in the following tables for each of the operations and and or. The third column of the table should be the value of (and arg1 arg2), where arg1 is the first argument and arg2 is the second argument. The fourth column should be the value of (or arg1 arg2).
arg1 arg2 (and arg1 arg2) (or arg1 arg2)
#f #f
#f #t
#t #f
#t #t
Exercise 2: Simple Color Predicates As you may recall, in the reading on Boolean values and predicate procedures, we defined two simple predicates, rgb-light? and rgb-dark?. Here is their code again. a. Test those predicates on a few extreme values, such as black, white, and a grey, to make sure that they work as you might expect. b. Determine experimentally whether there is a dark color with a blue component of 255. c. Determine experimentally the largest green component a color can have and still be considered dark. d. Determine experimentally the smallest green component a color can have and still be considered light.
Exercise 3: Writing Your Own Predicates a. Write a predicate, (not-very-blue? color), that holds only when the color's blue component is less than 64. b. Write a predicate, (red-dominates? color), that holds only if the red component is greater than the sum of the green and the blue components. c. Write a predicate, (greyish? color), that holds only if no two components of color differ by more than 8.
Exercise 4: Comparing Colors 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, (rgb-greener? color1 color2), that holds only if the green component of color1 is larger than the green component of color2. b. Write a two-parameter predicate, (rgb-lighter? color1 color2), that holds only if 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).
Exercise 5: Ranges Write a procedure, (valid-component? comp), that determines if the value named by comp is between 0 and 255, inclusive.
Exercise 6: Exploring <code>and</code> and <code>or</code> 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.
Exercise 7: Conditional Defines As you may recall from the end of the reading, a short piece of code can help you ensure that an image is defined. Let's consider a series of actions that might motivate the need for such code. a. In the interactions pane, create a new image called canvas. > (define canvas (image-new 300 100)) b. Click Run. c. You should recall having created an image. Is it available? Let's try to find out experimentally. Predict the result of the following expression. > (image-show canvas) d. As you may have predicted, and almost certainly discovered, although it feels like we just defined canvas, it is no longer available. Hence, we need to redefine it before showing it. > (define canvas (image-new 300 100)) > (image-show canvas) e. Should we always build a new image for canvas when we want to show it? Certainly not. If we already have canvas defined, we often want to keep the old definition. Hence, we use defined? to help us choose whether or not to build the image. Here's some code to do just that, which you should enter in the interactions pane. > (define canvas (or (and (defined? canvas) canvas) (image-show (image-new 300 100)))) What do you expect to have happen if we run this code? f. Check your answer experimentally. g. Click Run again. What do you expect to happen if we run the code in the interactions pane again? h. Check your answer experimentally. i. What do you expect to happen if we run the code in the interactions pane once again? Check your answer. j. Close the window for canvas. What effect do you expect the following instruction to have? > (image-set-pixel! canvas 0 0 (rgb-new 255 0 0)) k. Check your answer experimentally. l. You should have just discovered that even though we've created and shown canvas, once the window has been closed, canvas is no longer available. Look at the code from the reading, and explain how that code helps deal with the problem. (Hint! You may want to see what value image? returns before and after you close the window for an image.)
For Those With Extra Time
Extra 1: Exploring Shades, Revisited You may recall that in problem 2, you explored how certain components can affect whether a color is considered light and 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 rgb-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.)
Extra 2: Converting to Black, Grey, or White The reading included a procedure that used and and or to convert colors to black, grey, or red. 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, > (rgb->string (rgb-bgw (rgb-new 10 10 10))) b. Try building an image variant using rgb-bgw. For example, > (define kitten (image-load "/home/rebelsky/MediaScheme/Images/kitten.png")) > (image-show (image-variant kitten rgb-bgw)) c. Write a similar procedure, rgb-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 rgb-bgy.
Extra 3: Dominance Write three predicates, each of which checks if one component is greater than the others. (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.
Extra 4: Transforming to Dominant Colors a. Write a procedure, (rgb-dominant color), 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, rgb-dominant should convert the color to a moderate grey. You may want to use the structure of rgb-bgw as a pattern for this procedure. b. Try applying rgb-dominant to a variety of colors. c. Try building an image variant using rgb-dominant.
Notes
Notes on Problem 6 (and) has value true (#t) because and has a value of true if none of the parameters have value false. Since this calls has no parameters, none are false. Alternately, you can think of #t as the and-itive identity. That is, (and #t x) is x. (or) has value false (#f) because or has value false if none of the parameters is non-false. Since this call has no parameters, none are non-false. Alternately, you can think of #f as the or-itive identity. That is, (or #f x) is x.