;; The first three lines of this file were inserted by DrScheme. 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 functional_abstraction2) (read-case-sensitive #t) (teachpacks ((lib "testing.ss" "teachpack" "htdp"))) (htdp-settings #8(#t constructor repeating-decimal #f #t none #f ((lib "testing.ss" "teachpack" "htdp"))))) ;;Contract: (listof X) (X -> boolean) -> (listof X) ;; The function takes a list of X and a criterion function ;; and creates a list of elements that satisfy the criterion (define (filter2 criterion alist) (cond [(empty? alist) empty] [(criterion (first alist)) (cons (first alist) (filter2 criterion (rest alist)))] [else (filter2 criterion (rest alist))] ) ) (filter2 even? (list 1 2 3 4 5 6 7 8 9)) ;; expected value (list 2 4 6 8) ;; posn -> boolean ;; returns true if both x and y coordinates of a ;; position are between 0 and 300 (included) (define (within-canvas a-posn) (and (>= (posn-x a-posn) 0) (<= (posn-x a-posn) 300) (>= (posn-y a-posn) 0) (<= (posn-y a-posn) 300)) ) (define positions (list (make-posn 200 2000) (make-posn 150 230) (make-posn -100 20) (make-posn 400 50) (make-posn 100 300))) (filter2 within-canvas positions) ;; given a list of mixed data, use filter2 to create a list of all ;; symbols in the original list ;; given a list of mixed data, use filter2 to create a list of all ;; non-symbols in the original list