;; 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-reader.ss" "lang")((modname 16.3.4) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "dir.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "dir.ss" "teachpack" "htdp"))))) ;(define-struct file (name size content)) ;A file is a structure ;(make-file name size content) where name is a symbol, size is a number, and content is a Scheme datum. ;A list-of-files (lof) is either ;1. empty or ;2. (cons f lof) where f is a file and lof is a list-of-files. ;(define-struct dir (name dirs files)) ;A directory (dir) is a structure (make-dir n d f) where n is a symbol, d is a list-of-dirs, and f is a list-of-files. ; ;A list-of-dirs (lod) is either ;1. empty or ;2. (cons d lod) where d is a dir and lod is a list-of-dirs. ;Template ;fun-for-file : file -> ??? ;(define (fun-for-file a-file) ; (... (file-name a-file) ... ; ... (file-size a-file) ... ; ... (file-content a-file) ...)) ; ;fun-for-dir : dir -> ??? ;(define (fun-for-dir a-dir) ; (... (dir-name a-dir) ... ; ... (fun-for-lod (dir-dirs a-dir)) ... ; ... (fun-for-lof (dir-files a-dir)) ...)) ; ;fun-for-lof : lof -> ??? ;(define (fun-for-lof a-lof) ; (cond ; [(empty? a-lof) ...] ; [(cons? a-lof) ... (first a-lof) ... ; ... (fun-for-lof (rest a-lof)) ...])) ; ;fun-for-lod : lod -> ??? ;(define (fun-for-lod a-lod) ; (cond ; [(empty? a-lod) ...] ; [(cons? a-lod) ... (fun-for-dir (first a-lod)) ... ; ... (fun-for-lod (rest a-lod)) ...])) (define hang (make-file 'hang 8 empty)) (define draw-1 (make-file 'draw 2 empty)) (define read!-1 (make-file 'read! 19 empty)) (define part1 (make-file 'part1 99 empty)) (define part2 (make-file 'part2 52 empty)) (define part3 (make-file 'part3 17 empty)) (define read!-2 (make-file 'read! 10 empty)) (define Code (make-dir 'Code empty (list hang draw-1))) (define Docs (make-dir 'Docs empty (list read!-1))) (define Libs (make-dir 'Libs (list Code Docs) empty)) (define Text (make-dir 'Text empty (list part1 part2 part3))) (define TS (make-dir 'TS (list Text Libs) (list read!-2))) ;how-many : dir -> number ;Given a-dir, determine the number of files in the dir tree. (define (how-many a-dir) (+ (length (dir-files a-dir)) (how-many-lod (dir-dirs a-dir)))) ;how-many-lod : lod -> number ;Given a-lod, determine the number of files in the list of directories, counting files contained in all subdirectories. (define (how-many-lod a-lod) (cond [(empty? a-lod) 0] [(cons? a-lod) (+ (how-many (first a-lod)) (how-many-lod (rest a-lod)))])) ;du-dir : dir -> number ;Given a-dir, computes the total size (disk usage, du) of all files in the directory tree. Assumes that for a given directory, storing a file or a directory costs 1 unit of storage space. (define (du-dir a-dir) (+ (length (dir-dirs a-dir)) (length (dir-files a-dir)) (du-lod (dir-dirs a-dir)) (du-lof (dir-files a-dir)))) ;du-lof : lof -> number ;Given a-lof, compute the disk usage of the list-of-files. (define (du-lof a-lof) (cond [(empty? a-lof) 0] [(cons? a-lof) (+ (file-size (first a-lof)) (du-lof (rest a-lof)))])) ;du-lod : lod -> number (define (du-lod a-lod) (cond [(empty? a-lod) 0] [(cons? a-lod) (+ (du-dir (first a-lod)) (du-lod (rest a-lod)))])) ;find? : dir symbol -> boolean ;Given a-dir and a-filename, determine if the file is located within the dir tree. (define (find? a-dir a-filename) (or (find?-for-lod (dir-dirs a-dir) a-filename) (find?-for-lof (dir-files a-dir) a-filename))) ;find?-for-lof : lof symbol -> boolean (define (find?-for-lof a-lof a-filename) (cond [(empty? a-lof) false] [(cons? a-lof) (or (symbol=? a-filename (file-name (first a-lof))) (find?-for-lof (rest a-lof) a-filename))])) ;find?-for-lod : lod symbol -> boolean (define (find?-for-lod a-lod a-filename) (cond [(empty? a-lod) false] [(cons? a-lod) (or (find? (first a-lod) a-filename) (find?-for-lod (rest a-lod) a-filename))])) ;Data Definition: ;A path is a list of directory names (list-of-symbols). In this instance, a path is a list which contains the name of the directory being searched as well as the sub-directory (or subdirectories) containing the file. ; ;A list-of-symbols is either ;1. empty or ;2. (cons s los) where s is a symbol and los is a list-of-symbols. ;find : dir symbol -> path/false ;Given a-dir and a-filename, find the subdirectory of a-dir which contains the file with the corresponding name. Return a path containing a-dir as well as the subdirectory (or subdirectories) containing a-filename. If the file is not present in the dir tree, return false. (define (find a-dir a-filename) (cond [(find?-for-lof (dir-files a-dir) a-filename) (find-path-for-dir a-dir a-filename)] [(find? a-dir a-filename) (append (list (dir-name a-dir)) (find-path-for-dir a-dir a-filename))] [else false])) ;find-path-for-dir : dir symbol -> path (list-of-symbols) ;Given a-dir and a-filename, return the name of the subdirectory (or subdirectories) that contains a-filename as a list-of-symbols. If the file is not present in the dir tree, return empty. (define (find-path-for-dir a-dir a-filename) (cond [(find?-for-lof (dir-files a-dir) a-filename) (append (list (dir-name a-dir)) (find-path-for-lod (dir-dirs a-dir) a-filename))] [else (find-path-for-lod (dir-dirs a-dir) a-filename)])) ;find-path-for-lod : lod symbol -> path (list-of-symbols) ;Given a-lod and a-filename, return the name of the subdirectory (or subdirectories) that contains a-filename as a list-of-symbols. If the file is not present in the dir trees of the list-of-directories, return empty. (define (find-path-for-lod a-lod a-filename) (cond [(empty? a-lod) empty] [(cons? a-lod) (append (find-path-for-dir (first a-lod) a-filename) (find-path-for-lod (rest a-lod) a-filename))])) (find (create-dir "C:\\Documents and Settings\\Aaron Lin\\Desktop\\Scheme Test") '|file A.txt|)