;; 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 "2017-fall-reader.rkt" "csc104")((modname Mon-filter-local) (compthink-settings #hash((prefix-types? . #f)))) (define L (list (list "a" "b" "c") "d" "e" (list "f") (list "g" "h") "i")) ; Ok if we just want to know string vs not string ;(map string? L) ;(map list? L) ; (map image? L) ; How to get just the strings in L? ;(filter string? L) ;(filter list? L) ;(filter image? L) ;(filter length L) ; doesn't work! (require 2htdp/batch-io) ; magic incantation for read-words (define ALICE-LIST (read-words "alice30.txt")) (define ENGLISH-LIST (read-words "/usr/share/dict/words")) ; Goal: want to be able to get all words in a list ; that contain a particular substring ;(string-contains? "cat" "caterpillar") ;(string-contains? "cat" "camera") (check-expect (filter-by-string-contains (list "cat" "dog" "hat") "at") (list "cat" "hat")) ; imagine a simpler problem (define (contains-at? s) (string-contains? "at" s)) (filter contains-at? (list "cat" "dog" "hat")) (check-expect (filter-by-string-contains (list "cat" "dog" "hat") "at") (local [(define (p s) (string-contains? "at" s))] (filter p (list "cat" "dog" "hat")))) (define (filter-by-string-contains a-list a-string) (local [(define (p s) (string-contains? a-string s))] (filter p a-list))) #;(step (filter-by-string-contains (list "cat" "dog" "hat") "at")) (filter-by-string-contains ENGLISH-LIST "wombat") ; Goal: find all strings in a list that have length >= some value (check-expect (filter-by-string-length (list "a" "bc" "def") 2) (list "bc")) (check-expect (filter-by-string-length (list "a" "bc" "def") 2) (local [(define (p s) (= (string-length s) 2))] (filter p (list "a" "bc" "def")))) (define (filter-by-string-length a-list a-number) (local [(define (p s) (= (string-length s) a-number))] (filter p a-list))) (filter-by-string-contains (filter-by-string-length ENGLISH-LIST 3) "x")