;; Template for file: ;; file-handler: file --> ? ;; ;(define file-handler ; (lambda (f) ; (cond [(simple? f) ...(simple-name f)... ; ...(los-handler (simple-contents f))...] ; [(dir? f) ...(dir-name f)... ; ...(lof-handler (dir-files f))...]))) ;; ;; Note how when we ask "What sort of data is (simple-contents f)?", ;; the answer "list_of_symbol" suggests what function we should ;; hand it off to. ;;;;;;;; Repeated from examples.ss and templates.ss ;;;;;;;;;;; ;; The define-structs for the two types of files: ;; (define-struct simple (name contents)) (define-struct dir (name files)) ;; contains?: symbol, list_of_symbol --> bool ;; (define contains? (lambda (target los) (cond [(empty? los) #f] [else (or (eq? target (first los)) (contains? target (rest los)))]))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Start of solutions for grep, grep*, etc. ;; ;; grep : symbol * file -> bool ;; Returns whether the symbol is in the contents of the file. ;; (define grep (lambda (symbol file) (cond [(simple? file) (contains? symbol (simple-contents file))] [(dir? file) #f]))) ;; grep* : symbol * list_of_file -> bool ;; Returns whether the symbol is in the contents of the file, or in the ;; contents of one of the files in a directory. ;; (define grep* (lambda (symbol lof) (cond [(empty? lof) #f] [else (or (grep symbol (first lof)) (grep* symbol (rest lof)))]))) ;; grepr*-lof : symbol * list_of_file -> bool ;; Returns whether the symbol is in the contents of any file accessible. ;; from the list ;; (define grepr*-lof (lambda (symbol lof) (cond [(empty? lof) #f] [else (or (grepr* symbol (first lof)) (grepr*-lof symbol (rest lof)))]))) ;; grepr* : symbol * file -> bool ;; Returns whether the symbol is in the contents of any file accessible ;; from the file. ;; (define grepr* (lambda (symbol file) (cond [(simple? file) (contains? symbol (simple-contents file))] [(dir? file) (grepr*-lof symbol (dir-files file))])))