Warning This class is being recorded.
Approximate overview
Academic
Cultural
Peer
Wellness
Misc
We’re doing this live, probably switching back and forth between DrRacket and the eboard.
And yes, I’ll be asking you questions.
How does this work?
(define rotated-solid-rectangle
(lambda (stuff)
(apply (cut (rectangle <> <> "solid" <>))
stuff)))
When we apply a procedure to stuff, we make each element of the list a parameter to that procedure. So the width will be the first parameter, the height will be the second parameter, and the color will be the third parameter (at least if that’s how we put together the list).
The cut builds a procedure that needs three parameters. Conveniently,
they are the width, height, and color. And we can then apply it.
Whoops … some updates.
(define thin-shape (list 40 90 (rgb 255 192 128)))
(define rotated-solid-rectangle
(lambda (stuff)
(rotate 45
(apply (cut (rectangle <> <> "solid" <>))
stuff))))
(define thin-shape (list 40 90 (rgb 255 192 128)))
(define vary
(lambda (stuff)
(list stuff
(list (list-ref stuff 0) (list-ref stuff 1) (darker-color (list-ref stuff 2))))))
What is (vary thin-shape)?
shape-params? (a list of width height color)[Work elided]
> (vary thin-shape)
(list
(list 40 90 (color 255 192 128 255))
(list 40 90 (color 223 160 96 255))
(list 40 90 (color 192 128 255 255))
(list 40 90 (color 128 255 192 255))
(list 40 90 (color 160 96 223 255)))
> (map rotated-solid-rectangle (vary thin-shape))
(list . . . . .)
Suppose I write
(map vary (vary thin-shape))
What should I get (conceptually)?
(vary thin-shape) gets processed by vary.vary makes a list of a few (five) shape descriptions.How do we take a list of lists of shape descriptions and turn each shape description into a rotated-solid-rectangle?
Note: We can turn each shape description in a list of shape descriptions
into a rotated-solid-rectangle with (map rotated-solid-rectangle stuff).
We have a list of lists of shape descriptions. We want to process it, so let’s write it recursively.
;;; (rsr-1 lo-lo-sd) -> list-of list-of image?
;;; lo-lo-sd -> list-of list-of shape-params?
;;; Turn each shape-param into a rotated solid rectangle.
(define rsr-1
(lambda (lo-lo-sd)
(if BASE-CASE-TEST
BASE-CASE
(DO-SOMETHING (rsr-1 (SIMPLIFY lo-lo-sd))))))
;;; (rsr-1 lo-lo-sd) -> list-of list-of image?
;;; lo-lo-sd : list-of list-of shape-params?
;;; Turn each shape-param into a rotated solid rectangle.
(define rsr-1
(lambda (lo-lo-sd)
(if (null? lo-lo-sd)
null
(DO-SOMETHING (rsr-1 (cdr lo-lo-sd))))))
Detour: Let’s make it a little simpler
Our goal: We have a list of those weird shape-params? lists. We
want to make it into a list of images.
We know that we convert ONE shape-param? value into a rotated-solid-rectangle
with (rotated-solid-rectangle THING).
Other than map, how do we do that to each element of the list?
Answer: Recursion!
;;; (rsr-0 lo-sd) -> list-of image?
;;; lo-sd : list-of shape-param?
;;; Convert each shape-param? in the list to a rotated solid rectangle
(define rsr-0
(lambd (lo-sd)
(if (null? lo-sd)
null
(DO-SOMETHING (rsr-0 (cdr lo-sd))))))
What should DO-SOMETHING be?
car.;;; (rsr-0 lo-sd) -> list-of image?
;;; lo-sd : list-of shape-param?
;;; Convert each shape-param? in the list to a rotated solid rectangle
(define rsr-0
(lambd (lo-sd)
(if (null? lo-sd)
null
(cons (rotated-solid-rectangle (car lo-sd))
(rsr-0 (cdr lo-sd))))))
You may not use map.
;;; (rsr-1 lo-lo-sd) -> list-of list-of image?
;;; lo-lo-sd : list-of list-of shape-params?
;;; Turn each shape-param into a rotated solid rectangle.
(define rsr-1
(lambda (lo-lo-sd)
(if (null? lo-lo-sd)
null
(cons (rsr-0 (car lo-lo-sd))
(rsr-1 (cdr lo-lo-sd))))))
lo-lo-sd (which I get with car,
it is a list of shape descriptions.What if I have a list of (either (a) shape description or (b) a list of shape descriptions)?
(rsr-1 (list thin-shape
(make-list 4 thin-shape)
thin-shape))
It doesn’t match the requirements in the documentation. So we expect it to fail.
And we are right.
So, we want to write a better procedure.
;;; (rsr-1-improved stuff) -> list-of (either image? or list-of image?)
;;; stuff : list-of (either shape-params? or list-of shape-params?)
;;; Turn each shape-param into a rotated solid rectangle.
(define rsr-1-improved
(lambda (stuff)
(if (null? stuff)
null
(cons (if (shape-params? (car stuff))
(rotated-solid-rectangle (car stuff))
(rsr-0 (car stuff)))
(rsr-1-improved (cdr stuff))))))
You may assume that shape-params? is defined.
How do I pay tokens to turn something in late?
You will be charged automatically.
How do I tell how I’m doing on tokens?
Sam plans to update those this coming weekend.
Are you going to record the video?
Um … no. That’s why we did the discussion.
Skipped.