; ; ;; THIS DEFINITION IS BUILT-IN TO RACKET ; ; ;; filter: (alpha -> boolean) list-of-alpha -> list-of-alpha ; ;; consumes a function and a list of alpha and ; ;; extracts the elements of the list for which the given function returns true ; (define (filter keep? aloa) ; (cond [(empty? aloa) empty] ; [(cons? aloa) (cond [(keep? (first aloa)) ; (cons (first aloa) (filter (rest aloa)))] ; [else (filter keep? (rest aloa))])])) ; ; ;; extract-positives: list-of-number -> list-of-number ;; consumes a list of numbers and produces a list of only the positive numbers (define (extract-positives alon) (filter positive? alon)) ;; a dillo is a struct ;; (make-dillo number boolean) (define-struct dillo (length dead?)) (define DILLOS (list (make-dillo 3 true) (make-dillo 3 false) (make-dillo 5 false) (make-dillo 7 false))) ;; dead-dillos: list-of-dillo -> list-of-dillo ;; consumes a list of dillos and returns an list of the dead dillos (define (dead-dillos alod) (filter dillo-dead? alod)) ;; live-dillos: list-of-dillo -> list-of-dillo ;; consumes a list of dillos and returns a list of the live dillos (define (live-dillos alod) (filter live? alod)) ;; live?: dillo -> boolean ;; consumes a dillo and returns true if the dillo is alive (define (live? a-dillo) (not (dillo-dead? a-dillo))) ;; short-and-dead: list-of-dillo -> list-of-dillo ;; produces a list of dead dillos shorter than 5 (define (short-and-dead alod) (filter short-dead? alod)) ;; short-dead?: dillo -> boolean ;; consumes a dillo and returns true if it is dead and shorter than 5 units (define (short-dead? a-dillo) (and (< (dillo-length a-dillo) 5) (dillo-dead? a-dillo))) ;; short-strings: number list-of-string -> list-of-string ;; consumes a list of strings and produces a list of all those strings with fewer than ;; the given number of characters (define (short-strings limit alos) (local [(define (short? astr) (< (string-length astr) limit))] (filter short? alos)))