Blob


1 ;; The first three lines of this file were inserted by DrScheme. They record metadata
2 ;; about the language level of this file in a form that our tools can easily process.
3 #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")))))
4 ;(define-struct file (name size content))
6 ;A file is a structure
7 ;(make-file name size content) where name is a symbol, size is a number, and content is a Scheme datum.
9 ;A list-of-files (lof) is either
10 ;1. empty or
11 ;2. (cons f lof) where f is a file and lof is a list-of-files.
13 ;(define-struct dir (name dirs files))
15 ;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.
16 ;
17 ;A list-of-dirs (lod) is either
18 ;1. empty or
19 ;2. (cons d lod) where d is a dir and lod is a list-of-dirs.
21 ;Template
22 ;fun-for-file : file -> ???
23 ;(define (fun-for-file a-file)
24 ; (... (file-name a-file) ...
25 ; ... (file-size a-file) ...
26 ; ... (file-content a-file) ...))
27 ;
28 ;fun-for-dir : dir -> ???
29 ;(define (fun-for-dir a-dir)
30 ; (... (dir-name a-dir) ...
31 ; ... (fun-for-lod (dir-dirs a-dir)) ...
32 ; ... (fun-for-lof (dir-files a-dir)) ...))
33 ;
34 ;fun-for-lof : lof -> ???
35 ;(define (fun-for-lof a-lof)
36 ; (cond
37 ; [(empty? a-lof) ...]
38 ; [(cons? a-lof) ... (first a-lof) ...
39 ; ... (fun-for-lof (rest a-lof)) ...]))
40 ;
41 ;fun-for-lod : lod -> ???
42 ;(define (fun-for-lod a-lod)
43 ; (cond
44 ; [(empty? a-lod) ...]
45 ; [(cons? a-lod) ... (fun-for-dir (first a-lod)) ...
46 ; ... (fun-for-lod (rest a-lod)) ...]))
48 (define hang (make-file 'hang 8 empty))
49 (define draw-1 (make-file 'draw 2 empty))
50 (define read!-1 (make-file 'read! 19 empty))
51 (define part1 (make-file 'part1 99 empty))
52 (define part2 (make-file 'part2 52 empty))
53 (define part3 (make-file 'part3 17 empty))
54 (define read!-2 (make-file 'read! 10 empty))
55 (define Code (make-dir 'Code empty (list hang draw-1)))
56 (define Docs (make-dir 'Docs empty (list read!-1)))
57 (define Libs (make-dir 'Libs (list Code Docs) empty))
58 (define Text (make-dir 'Text empty (list part1 part2 part3)))
59 (define TS (make-dir 'TS (list Text Libs) (list read!-2)))
61 ;how-many : dir -> number
62 ;Given a-dir, determine the number of files in the dir tree.
63 (define (how-many a-dir)
64 (+ (length (dir-files a-dir))
65 (how-many-lod (dir-dirs a-dir))))
67 ;how-many-lod : lod -> number
68 ;Given a-lod, determine the number of files in the list of directories, counting files contained in all subdirectories.
69 (define (how-many-lod a-lod)
70 (cond
71 [(empty? a-lod) 0]
72 [(cons? a-lod) (+ (how-many (first a-lod))
73 (how-many-lod (rest a-lod)))]))
75 ;du-dir : dir -> number
76 ;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.
78 (define (du-dir a-dir)
79 (+ (length (dir-dirs a-dir))
80 (length (dir-files a-dir))
81 (du-lod (dir-dirs a-dir))
82 (du-lof (dir-files a-dir))))
84 ;du-lof : lof -> number
85 ;Given a-lof, compute the disk usage of the list-of-files.
86 (define (du-lof a-lof)
87 (cond
88 [(empty? a-lof) 0]
89 [(cons? a-lof) (+ (file-size (first a-lof))
90 (du-lof (rest a-lof)))]))
92 ;du-lod : lod -> number
93 (define (du-lod a-lod)
94 (cond
95 [(empty? a-lod) 0]
96 [(cons? a-lod) (+ (du-dir (first a-lod))
97 (du-lod (rest a-lod)))]))