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-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")))))
4 ;A directory (dir) is a structure
5 ;(make-dir name content size systems) where name, systems are symbols,
6 ;size is a number, and content is a list-of-files-and-directories (LOFD).
7 ;
8 ;A list-of-files-and-directories (LOFD) is either
9 ;1. empty,
10 ;2. (cons file lofd)
11 ;3. (cons dir lofd),
12 ;where file is a symbol, dir is a directory,
13 ;and lofd is a LOFD (list-of-files-and-directories).
15 (define-struct dir (name content size systems))
17 ;;State variables
19 ;;how-many-directories : N
20 ;;The number of subdirectories encountered
21 (define how-many-directories 0)
23 ;Template
24 ;fun-for-dir : dir -> ???
25 ;(define (fun-for-dir a-dir)
26 ; (... (dir-name a-dir) ...
27 ; ... (fun-for-lofd (dir-content a-dir)) ...))
28 ;
29 ;
30 ;fun-for-lofd : lofd -> ???
31 ;(define (fun-for-lofd a-lofd)
32 ; (cond
33 ; [(empty? a-lofd) ...]
34 ; [(symbol? (first a-lofd)) ... (first a-lofd) ...
35 ; ... (fun-for-lofd (rest a-lofd)) ...]
36 ; [(dir? (first a-lofd)) ... (fun-for-dir (first a-lofd)) ...
37 ; ... (fun-for-lofd (rest a-lofd)) ...]))
39 ;Examples:
40 (define Code (make-dir 'Code '(hang draw) 5 'ReadOnly))
41 (define Docs (make-dir 'Docs '(read!) 5 'ReadWrite))
42 (define Libs (make-dir 'Libs (list Code Docs) 5 'Executable))
43 (define Text (make-dir 'Text '(part1 part2 part3) 5 'ReadOnly))
44 (define TS (make-dir 'TS (list Text Libs 'read!) 5 'None))
46 ;;dir-listing : dir -> lofd
47 ;;Consumes a directory and produces a list of all file names in the directory and all of its subdirectories
48 ;;Effect: Count the number of subdirectories accessed and store it in how-many-directories.
50 ;;list-all-filenames : lofd -> (listof symbol)
51 ;;Lists the names of all the files in the directory.
52 ;;Effect: everytime a directory is accessed, add one more to how-many-directories.
54 (define (dir-listing adir)
55 (local ((define (list-all-filenames alofd)
56 (cond
57 [(empty? alofd) empty]
58 [(symbol? (first alofd)) (cons (first alofd) (list-all-filenames (rest alofd)))]
59 [else (begin (set! how-many-directories (add1 how-many-directories))
60 (append (list-all-filenames (dir-content (first alofd)))
61 (list-all-filenames (rest alofd))))])))
62 (begin (set! how-many-directories 0)
63 (list-all-filenames (dir-content adir)))))
67 (dir-listing TS)