;; contains-doll? : los -> boolean ;; to determine whether alos contains ;; the symbol 'doll (define (contains-doll? alos) (cond [(empty? alos) false] [else (cond [(symbol=? (first alos) 'doll) true] [else (contains-doll? (rest alos))])])) ;; contains-car? : los -> boolean ;; to determine whether alos contains ;; the symbol 'car (define (contains-car? alos) (cond [(empty? alos) false] [else (cond [(symbol=? (first alos) 'car) true] [else (contains-car? (rest alos))])])) ;; contains? : symbol los -> boolean ;; to determine whether alos contains the symbol s (define (contains? s alos) (cond [(empty? alos) false] [else (cond [(symbol=? (first alos) s) true] [else (contains? s (rest alos))])])) ;; below : lon number -> lon ;; to construct a list of those numbers ;; on alon that are below t (define (below alon t) (cond [(empty? alon) empty] [else (cond [(< (first alon) t) (cons (first alon) (below (rest alon) t))] [else (below (rest alon) t)])])) ;; above : lon number -> lon ;; to construct a list of those numbers ;; on alon that are above t (define (above alon t) (cond [(empty? alon) empty] [else (cond [(> (first alon) t) (cons (first alon) (above (rest alon) t))] [else (above (rest alon) t)])])) ;; filter-above-or-below : lon number symbol -> lon ;; constructs a list of all numbers in alon that ;; are: ;; if symbol is 'above - all numbers above number ;; if symbols is 'below - all numbers below number ;; (define (filter-above-or-below alon t aorb) (cond [(empty? alon) empty] [else (cond [(symbol=? 'above aorb) (cond [(> (first alon) t) (cons (first alon) (filter-above-or-below (rest alon) t aorb))] [else (filter-above-or-below (rest alon) t aorb)])] [(symbol=? 'below aorb) (cond [(< (first alon) t) (cons (first alon) (filter-above-or-below (rest alon) t aorb))] [else (filter-above-or-below (rest alon) t aorb)])])])) ;; (above '(8 1 7 253 49 26 4) 17) ;; (below '(8 1 7 253 49 26 4) 17) ;; better function (define (filter1 rel-op alon t) (cond [(empty? alon) empty] [else (cond [(rel-op (first alon) t) (cons (first alon) (filter1 rel-op (rest alon) t))] [else (filter1 rel-op (rest alon) t)])])) ;;(filter1 < '(8 1 7 253 49 26 4) 17) ;;(filter1 > '(8 1 7 253 49 26 4) 17) ;; is x a multiple of y (both integers) ? (define (multiple? x y) (= (/ x y) (floor (/ x y)))) (filter1 multiple? '(4 18 17 25 30 88) 3) (define (minimum alon) (cond [(empty? (rest alon)) (first alon)] [else (local ((define minrest (minimum (rest alon)))) (cond [(< (first alon) minrest) (first alon)] [else minrest]))])) ;; filter2 consumes an operator and a list of numbers and ;; produces a list of numbers. ;; (filter2 op alon) applies a unary (boolean) operator op ;; to each number in alon. Each element of alon that is ;; true according to op is included in the produced list. ;; (define (filter2 op alon) (cond [(empty? alon) empty] [(op (first alon)) (cons (first alon) (filter2 op (rest alon)))] [else (filter2 op (rest alon))])) ;; posn operator that returns true when given a posn ;; that contains an x value of 0 (define (posn-zero-x p) (= 0 (posn-x p))) ;; test code for the posn operator. (filter2 posn-zero-x (list (make-posn 3 4) (make-posn 0 5) (make-posn 0 200) (make-posn 200 0) (make-posn 0 0) (make-posn 13 22))) ;; symbol? is already the operator we need for extracting ;; symbols from a list. (filter2 symbol? (list 1 3 'Hello (make-posn 2 3) 3.14159 'World)) ;; ;; filter1 operator that checks whether a symbol is in a set of symbols ;; ;; member? : symbol list of symbols => boolean (define (member? asym alos) (cond [(empty? alos) false] [(symbol=? asym (first alos)) true ] [else (member? asym (rest alos))])) (filter1 member? '(Hello world) '(Hi Hello Bojour))