;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname |map and filter|) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) ;; Helene Martin, Garfield high school ;; 10/6/2010 ;; Map/filter and anonymous functions ;; rewrite extract-seniors using filter (define (extract-seniors2 a-los) (filter senior? a-los)) (check-expect (extract-seniors2 STUDENTS) (cons ADAM (cons QUYNH empty))) ;; extract-positives : list-of-number -> list-of-number ;; consumes list of num and produces list of all positive nums in list ;; (uses built-in operator positive?) (define (extract-positives alon) (cond [(empty? alon) empty] [(cons? alon) (cond [(positive? (first alon)) (cons (first alon) (extract-positives (rest alon)))] [else (extract-positives (rest alon))])])) (define LIST3 (list 6 5 -4 8 -3)) (check-expect (extract-positives LIST3) (list 6 5 8)) ;; extract-positives2: list-of-number -> list-of-number (define (extract-positives2 alon) (filter positive? alon)) (check-expect (extract-positives2 LIST3) (list 6 5 8)) ;; short-strings : number list-of-string -> list-of-string ;; consumes num and list of string and produces list of all strings shorter than given num (define (short-strings limit alos) (cond [(empty? alos) empty] [(cons? alos) (cond [(< (string-length (first alos)) limit) (cons (first alos) (short-strings limit (rest alos)))] [else (short-strings limit (rest alos))])])) (define LIST4 (list "Garfield" "apple" "a" "the")) (check-expect (short-strings 4 LIST4) (list "a" "the")) (define (short-strings2 limit alos) (filter (lambda (a-string) (< (string-length a-string) limit)) alos)) (check-expect (short-strings2 4 LIST4) (list "a" "the"))