;;; extract-positives: ListOfNumber -> ListOfNumber ;;; produces a list of only the positive numbers ;(define (extract-positives alon) ; (cond [(empty? alon) empty] ; [(cons? alon) (if (positive? (first alon)) ; (cons (first alon) (extract-positives (rest alon))) ; (extract-positives (rest alon)))])) ; ;;; short-strings: Number ListOfString -> ListOfString ;;; produce a list of those strings with fewer than the given number of characters ;(define (short-strings limit alos) ; (cond [(empty? alos) empty] ; [(cons? alos) (if (< (string-length (first alos)) limit) ; (cons (first alos) (short-strings limit (rest alos))) ; (short-strings limit (rest alos)))])) ; ;;THIS FUNCTION IS BUILT INTO RACKET ; ;;; filter: (X -> Boolean) ListOfX -> ListOfX ;;; extracts the elements of the list for which the given function keep? returns true ;(define (filter keep? aloa) ; (cond [(empty? aloa) empty] ; [(cons? aloa) ; (if (keep? (first aloa)) ; (cons (first aloa) (filter keep? (rest aloa))) ; (filter keep? (rest aloa)))])) ;; extract-positives: ListOfNumber -> ListOfNumber ;; produces a list of only the positive numbers (define (extract-positives alon) (filter positive? alon)) (define-struct dillo (length dead?)) ;; a Dillo is a (make-dillo Natural Boolean) ;; ... ;; dead-dillos: ListOfDillo -> ListOfDillo ;; produces a list of only the dead dillos (define (dead-dillos alod) (filter dillo-dead? alod)) ;; live-dillos: ListOfDillo -> ListOfDillo ;; produces a list of only the live dillos (define (live-dillos alod) (filter live? alod)) ;; live?: Dillo -> Boolean ;; produces true if the given dillo is alive (define (live? adillo) (not (dillo-dead? adillo))) ;; short-and-dead: ListOfDillo -> ListOfDillo ;; produces a list of dead dillos less than 5 units in length (define (short-and-dead alod) (filter short-dead? alod)) ;; short-dead?: Dillo -> Boolean ;; true if the dillo is dead and less than 5 units (define (short-dead? adillo) (and (dillo-dead? adillo) (< (dillo-length adillo) 5))) (define DILLOS (list (make-dillo 3 true) (make-dillo 3 false) (make-dillo 5 false) (make-dillo 7 false) (make-dillo 7 true))) ;; short-strings: Number ListOfString -> ListOfString ;; produce a list of 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)))