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.
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.
; 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.
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.
; 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))
; 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))
Optional: handle mouse events, by moving the point to the mouse location.
Optional: handle key events, by removing the point.
; 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.