;; 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-advanced-reader.ss" "lang")((modname |37.3|) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))))) ;A directory (dir) is a structure ;(make-dir name content size systems) where name, systems are symbols, ;size is a number, and content is a list-of-files-and-directories (LOFD). ; ;A list-of-files-and-directories (LOFD) is either ;1. empty, ;2. (cons file lofd) ;3. (cons dir lofd), ;where file is a symbol, dir is a directory, ;and lofd is a LOFD (list-of-files-and-directories). (define-struct dir (name content size systems)) ;;State variables ;;how-many-directories : N ;;The number of subdirectories encountered (define how-many-directories 0) ;Template ;fun-for-dir : dir -> ??? ;(define (fun-for-dir a-dir) ; (... (dir-name a-dir) ... ; ... (fun-for-lofd (dir-content a-dir)) ...)) ; ; ;fun-for-lofd : lofd -> ??? ;(define (fun-for-lofd a-lofd) ; (cond ; [(empty? a-lofd) ...] ; [(symbol? (first a-lofd)) ... (first a-lofd) ... ; ... (fun-for-lofd (rest a-lofd)) ...] ; [(dir? (first a-lofd)) ... (fun-for-dir (first a-lofd)) ... ; ... (fun-for-lofd (rest a-lofd)) ...])) ;Examples: (define Code (make-dir 'Code '(hang draw) 5 'ReadOnly)) (define Docs (make-dir 'Docs '(read!) 5 'ReadWrite)) (define Libs (make-dir 'Libs (list Code Docs) 5 'Executable)) (define Text (make-dir 'Text '(part1 part2 part3) 5 'ReadOnly)) (define TS (make-dir 'TS (list Text Libs 'read!) 5 'None)) ;;dir-listing : dir -> lofd ;;Consumes a directory and produces a list of all file names in the directory and all of its subdirectories ;;Effect: Count the number of subdirectories accessed and store it in how-many-directories. ;;list-all-filenames : lofd -> (listof symbol) ;;Lists the names of all the files in the directory. ;;Effect: everytime a directory is accessed, add one more to how-many-directories. (define (dir-listing adir) (local ((define (list-all-filenames alofd) (cond [(empty? alofd) empty] [(symbol? (first alofd)) (cons (first alofd) (list-all-filenames (rest alofd)))] [else (begin (set! how-many-directories (add1 how-many-directories)) (append (list-all-filenames (dir-content (first alofd))) (list-all-filenames (rest alofd))))]))) (begin (set! how-many-directories 0) (list-all-filenames (dir-content adir))))) (dir-listing TS)