Functional Problem Solving (CSC 151 2014F) : EBoards

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


What is going to be on the quiz tomorrow?

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).