8.12

### Lecture 10: Unions of structuresðŸ”—

You don’t need to submit anything for this lecture. But we strongly encourage you to submit it as soon as possible using Handin as assignment lecture10, to practice and get automatic feedback.

#### 1Structure reviewðŸ”—

Let’s begin by reviewing how to design functions that work on structures. If you’re not confident with this, work through Lecture 8: Structures, Lab 4: Structures, and Lecture 9: More structures again.

Exercise 1.
 ; A Point is (make-point Number Number) (define-struct point [x y]) ; process-point : Point -> ...

Finish writing the template for processing a Point. Make it look like a function called process-point, and do not put it in a comment.

#### 2Enumerations + structures = unionsðŸ”—

What if we want to remove a point, or start an animation without one?

Previously, all of the data definitions we’ve seen with one of included numbers or strings. Now we’re going to extend that to unions, which include multiple kinds of structures.

 ; A MaybePoint is one of: ; - (make-none) ; - (make-point Number Number) (define-struct none []) (define-struct point [x y]) ; process-maybepoint : MaybePoint -> ...

Exercise 2. Finish the template for processing a MaybePoint. Make it look like a function called process-maybepoint, and do not put it in a comment.

#### 3Designing with unionsðŸ”—

 ; A MaybePoint is one of: ; - (make-none) ; - (make-point Number Number) (define-struct none []) (define-struct point [x y]) (require 2htdp/image) (define marker (circle 50 "solid" "red")) (define background (empty-scene 500 500))

Exercise 3. Finish designing the draw-maybepoint function:
 ; draw-maybepoint : MaybePoint -> Image ; place a marker at the given point, if any (check-expect (draw-maybepoint (make-none)) background) (check-expect (draw-maybepoint (make-point 70 400)) (place-image marker 70 400 background))

Exercise 4. Design the function discount-maybepoint, which takes a MaybePoint and produces another MaybePoint where all the numbers are 90% as big. If there are no numbers in the input, there should be no numbers in the output.

Exercise 5. Create an animation using big-bang, using the draw-maybepoint (for drawing) and discount-maybepoint (for ticks) functions you’ve already designed.

Optional: handle mouse events, by moving the point to the mouse location.

Optional: handle key events, by removing the point.

#### 4Nested structuresðŸ”—

Recall the definition of CoupleOfPoints from the video.

 ; A CoupleOfPoints is one of: ; - (make-none) ; - (make-one Point) ; - (make-two Point Point) (define-struct none []) (define-struct one [first]) (define-struct two [first second]) ; process-cp : CoupleOfPoints -> ... (define (process-cp cp) (cond [(none? cp) ...] [(one? cp) (... (process-point (one-first cp)))] [(two? cp) (... (process-point (two-first cp)) (process-point (two-second cp)))]))

Exercise 6. Design the function draw-coupleofpoints, which draws all the Points in a CoupleOfPoints on a background.

Optional: Read Chapter 6 of the textbook.