Warning! You are being recorded (and transcribed) (provided the technology is working correctly).
Approximate overview
Academic/Scholarly
Cultural
Peer
Wellness
Misc
This problem gave the most difficulty. So we’re going to talk through a similar problem.
First, let’s review what the two procedures do.
cut builds a procedure by “cutting out” parts of an expression. You
can also think about it as filling in some, but not all, parameters
to a procedure.compose (o) sequences unary procedures, creating a new procedure
that applies them right to left.Let’s consider an example.
(define rgb-fun-one
(lambda (color)
(rgb-subtract (rgb 255 255 255)
(rgb-darker
(rgb-darker
(rgb-subtract (rgb 255 255 255)
(rgb-lighter color)))))))
TPS: Describe in English what’s happening.
Decomposition suggests we should think about building that “subtract from white”
(define subtract-from-white
(lambda (color)
(rgb-subtract (rgb 255 255 255) color)))
(define rgb-fun-two
(o subtract-from-white
rgb-darker
rgb-darker
subtract-from-white
rgb-lighter))
Where’s the lambda? Where’s the parameter?
Every composition has an implicit parameter. (o fun1 fun2 fun3) is
“a function that takes one value, applies fun3 to it, applies fun2
to the result, and applies fun1 to that result.
How do you know what type is appropriate as the input?
It’s whatever is appropriate for the last function in the composition.
The documentation tells us.
;;; (rgb-fun-two color) -> rgb?
;;; color : rgb?
;;; Do something fun to `color`.
Detour: How does subtract-from-white work? Where are the calls to
rgb-red, rgb-green, and rgb-blue?
They are hidden in the implementation of
rgb-subtract.rgb-subtractextracts components from each color, then subtracts the corresponding components from each other, and then combines them into an rgb color.
(define rgb-subtract
(lambda (c1 c2)
(rgb (- (rgb-red c1) (rgb-red c2))
(- (rgb-green c1) (rgb-green c2))
(- (rgb-blue c1) (rgb-blue c2))
(rgb-alpha c1))))
What is rgb-alpha?
The alpha component is the “opacity” of a color when you overlay things. 255 is “completely opaque”, 0 is “completely transparent”.
Demo on laptop
> (overlay (solid-circle 200 (rgb 255 0 0 255))
(solid-square 200 (rgb 0 0 0 255)))
.
> (overlay (solid-circle 200 (rgb 255 0 0 192))
(solid-square 200 (rgb 0 0 0 255)))
.
> (overlay (solid-circle 200 (rgb 255 0 0 128))
(solid-square 200 (rgb 0 0 0 255)))
.
> (overlay (solid-circle 200 (rgb 255 0 0 64))
(solid-square 200 (rgb 0 0 0 255)))
Where’s the parameter to rgb-fun-two?
We can write subtract-from-white using cut.
(define subtract-from-white
(lambda (color)
(rgb-subtract (rgb 255 255 255) color)))
(define subtract-from-white
(cut (rgb-subtract (rgb 255 255 255) <>)))
And then plug that into rgb-fun-two
(define rgb-fun-three
(o (cut (rgb-subtract (rgb 255 255 255) <>))
rgb-darker
rgb-darker
(cut (rgb-subtract (rgb 255 255 255) <>))
rgb-lighter))
Why do functional programmers prefer this to the original one, even
though we’ve lost the helpful lambda (color)?
It’s more concise.
Fewer parentheses. We love parentheses, but in moderation.
With fewer parentheses, it may be easier to modify/edit/fix.
It may even be a bit more readable.
Here’s another example.
(define munge-shape
(lambda (shape)
(rotate
(beside (solid-square 10 "black")
(recolor
(rotate
(scale shape 2)
45)
"red"))
-45)))
How should we write this with cut and compose?
Hint: Think about the steps, then write out the individual steps with cut, then combine them with compose.
(cut (scale <> 2))(cut (rotate <> 45))
(rotate (cut (scale <> 2)) 45))(cut (recolor <> "red"))(cut (beside (solid-square 10 "black") <>))(cut (rotate <> -45))(define new-munge-shape
(o (cut (rotate <> -45))
(cut (beside (solid-square 10 "black") <>))
(cut (recolor <> "red"))
(cut (rotate <> 45))
(cut (scale <> 2))))
Would we ever write a compose inside of a cut?
Sure.
Here’s a bad example.
> (define rgbfun->namefun (cut (o rgb->color-name <> color-name->rgb)))
> (define color-name-darker (rgbfun->namefun rgb-darker))
> (color-name-darker "blue")
"blue"
> (color-name-darker "white")
"whitesmoke"
> (color-name-darker "yellow")
"yellow"
> (define color-name-darker (rgbfun->namefun (o rgb-darker rgb-darker)))
> (color-name-darker "white")
"gainsboro"
> (color-name-darker "yellow")
"gold"
> (color-name-darker "gold")
"orange"
A hint: You are much better off writing a trivial procedure, like the following, than submitting nothing for part of the assignment.
; I know this isn't write
(define solid-octagon
(lambda (side-length color)
(solid-square side-length color)))
This will probably pass the one-star tests, helping ensure that you get an R (which doesn’t require a token for the first resubmit) than an I (which does).
Many of you wrote something like the following for solid-pentagon.
(define solid-pentagon
(lambda (side-length color)
(solid-polygon (list (pt (real-part (make-polar (/ side-length (* 2 (sin (/ pi 5))))
(* 2/5 pi)))
(imag-part (make-polar (/ side-length (* 2 (sin (/ pi 5))))
(* 2/5 pi))))
(pt (real-part (make-polar (/ side-length (* 2 (sin (/ pi 5))))
(* 4/5 pi)))
(imag-part (make-polar (/ side-length (* 2 (sin (/ pi 5))))
(* 4/5 pi))))
(pt (real-part (make-polar (/ side-length (* 2 (sin (/ pi 5))))
(* 6/5 pi)))
(imag-part (make-polar (/ side-length (* 2 (sin (/ pi 5))))
(* 6/5 pi))))
(pt (real-part (make-polar (/ side-length (* 2 (sin (/ pi 5))))
(* 8/5 pi)))
(imag-part (make-polar (/ side-length (* 2 (sin (/ pi 5))))
(* 8/5 pi))))
(pt (real-part (make-polar (/ side-length (* 2 (sin (/ pi 5))))
(* 10/5 pi)))
(imag-part (make-polar (/ side-length (* 2 (sin (/ pi 5))))
(* 10/5 pi)))))
color)))
Isn’t the repetitious? What could you do to make it less so?
pentagon-point-one, pentagon-point-two,
etc.let
or map.What do you see as the key concepts that this assignment is asking you to learn or develop?
Please think about big-picture issues and what we’ve been doing lately.
Without looking at any resources (e.g., readings, labs, your notes), write down everything you know that might be useful for this assignment.
This should really be a brain dump. Write procedures. Write procedure names.
What resources (e.g., individual procedures you’ve written, readings, labs) will be helpful as you work on this assignment?
I’d like to see more notes on individual procedures. Now’s the time to look some up.
How long do you expect this assignment to take?
Please express this in hours, not days or weeks.
Can you go over the darker circles problem?
Sure.
;;; (thickly-outlined-circle color) -> image?
;;; color : color?
;;; Make a thickly outlined circle of the specified color.
(define thickly-outlined-circle
(lambda (color)
(overlay (outlined-circle 20 "black" 5)
(solid-circle 20 color))))
(define rainbow-colors
(list "red" "orange" "yellow" "green" "blue" "indigo" "violet"))
b. Using apply and map, make a picture of seven outlined circles in darker versions of the rainbow colors (computed by using two calls to rgb-darker). Note that you’ll need to convert the color names to RGB colors with color-name->rgb and then make them darker with two calls to rgb-darker.
If you can’t apply darker to a color name, you have to make the name into an RGB. Then you can make those darker. And then darker again. And then make those colors into outlined circles.
> (map (o thickly-outlined-circle rgb-darker rgb-darker color-name->rgb)
rainbow-colors)
'(. . . . . . .)
> (apply beside (map (o thickly-outlined-circle rgb-darker rgb-darker color-name->rgb)
rainbow-colors))
.