Problem 5. Mrs. Holly Wood, your boss, wants to finish a software package for Quazar, a company that produces animated movies. She would like you to design one function, though of course, you don't even know what she has been doing for the last few weeks. So she drops the following data definitions in your lap and leaves for the weekend:
;; A LOP is one of: ;; -- empty ;; -- (cons Posn LOP)
Recall that a Posn is a pre-defined class of data:
;; A Posn is a structure: ;; -- (make-posn Number Number)
Her last remark is:
Design the function good-bad. It consumes an LOP and produces one keeping just those Posns whose y coordinate is between 0 and 1000 (inclusive).
Solution:
;; Grader: Richard ;; good-bad : LOP -> LOP [1pt] ;; eliminate all those Posns ;; whose y component isn't between 0 and 1000 [1pt] (define (good-bad l) (cond ;; [2pt for cond] [(empty? l) empty] [else (cond ;; [2pt for nested cond; ;; 2pts if in-lined good?] [(good? (first l)) (cons (first l) (good-bad (rest l)))] [else (good-bad (rest l))])])) ;; Posn -> Boolean [1pt] ;; is the given Posn good? (define (good? p) (<= 0 (posn-y p) 1000)) ;; [1pt] ;; Examples/Tests: [PT 2, for example that throws out stuff] (equal? (good? (make-posn 10 -1)) false) (equal? (good? (make-posn 10 400)) true) (equal? (good? (make-posn 10 1100)) false) (equal? (good-bad (cons (make-posn 10 -1) (cons (make-posn 10 400) (cons (make-posn 10 1100) empty)))) (cons (make-posn 10 400) empty))