Blame


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