;; 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_abstraction) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "testing.ss" "teachpack" "htdp"))) (htdp-settings #8(#t constructor repeating-decimal #f #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "testing.ss" "teachpack" "htdp"))))) ;; contains5? : lon -> boolean ;; to determine whether alon contains ;; a number 5 (define (contains5? alon) (cond [(empty? alon) false] [else (cond [(= (first alon) 5) true] [else (contains5? (rest alon))])])) (check-expect (contains5? (list 1 2 3 4 5 6)) true) (check-expect (contains5? (list 1 2 3 4 6)) false) ;; contains3? : lon -> boolean ;; to determine whether alon contains ;; a number 3 (define (contains3? alon) (cond [(empty? alon) false] [else (cond [(= (first alon) 3) true] [else (contains3? (rest alon))])])) (check-expect (contains3? (list 1 2 3 4 5 6)) true) (check-expect (contains3? (list 1 2 4 6)) false) ;; Can we do it in one function? ;; contains3? : lon -> boolean ;; to determine whether alon contains ;; a number n (define (contains? alon n) (cond [(empty? alon) false] [else (cond [(= (first alon) n) true] [else ;; need to add n to the recursive call: (contains? (rest alon) n)])])) (check-expect (contains? (list 1 2 3 4 5 6) 5) true) (check-expect (contains? (list 1 2 3 4 6) 5) false) (check-expect (contains? (list 1 2 3 4 5 6) 3) true) (check-expect (contains? (list 1 2 4 6) 3) false) ;; A more advanced example of functional abstraction: ;; 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)])])) (check-expect (below (list 2 5 4 6 7 3) 5) (list 2 4 3)) ;; 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)])])) (check-expect (above (list 2 5 4 6 7 3) 5) (list 6 7)) ;; How can we combine those into one function? (define (filter1 relation alon t) (cond [(empty? alon) empty] [else (cond [(relation (first alon) t) (cons (first alon) (filter1 relation (rest alon) t))] [else (filter1 relation (rest alon) t)])])) (check-expect (filter1 < (list 2 5 4 6 7 3) 5) (list 2 4 3)) (check-expect (filter1 > (list 2 5 4 6 7 3) 5) (list 6 7)) (check-expect (filter1 = (list 2 5 4 6 7 3) 5) (list 5)) ;; what if I want all numbers not equal to 5? ;; I need to define a function (define (not-eq? a b) (not (= a b))) (check-expect (filter1 not-eq? (list 2 5 4 6 7 3) 5) (list 2 4 6 7 3)) ;; what else can use this function for? (check-expect (filter1 symbol=? (list 'a 'b 'c 'A 'B 'C) 'b) (list 'b)) ;; all symbols 'B or 'b? ;; all symbols except 'b or 'B?