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.2.3) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.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 ;Template
18 ;fun-for-dir : dir -> ???
19 ;(define (fun-for-dir a-dir)
20 ; (... (dir-name a-dir) ...
21 ; ... (fun-for-lofd (dir-content a-dir)) ...))
22 ;
23 ;
24 ;fun-for-lofd : lofd -> ???
25 ;(define (fun-for-lofd a-lofd)
26 ; (cond
27 ; [(empty? a-lofd) ...]
28 ; [(symbol? (first a-lofd)) ... (first a-lofd) ...
29 ; ... (fun-for-lofd (rest a-lofd)) ...]
30 ; [(dir? (first a-lofd)) ... (fun-for-dir (first a-lofd)) ...
31 ; ... (fun-for-lofd (rest a-lofd)) ...]))
33 ;Examples:
34 (define Code (make-dir 'Code '(hang draw) 5 'ReadOnly))
35 (define Docs (make-dir 'Docs '(read!) 5 'ReadWrite))
36 (define Libs (make-dir 'Libs (list Code Docs) 5 'Executable))
37 (define Text (make-dir 'Text '(part1 part2 part3) 5 'ReadOnly))
38 (define TS (make-dir 'TS (list Text Libs 'read!) 5 'None))
40 ;how-many : dir -> number
41 ;Given a-dir, determine the number of
42 ;files in the dir tree.
44 (define (how-many a-dir)
45 (how-many-lofd (dir-content a-dir)))
48 ;how-many-lofd : lofd -> number
49 ;Given a-lofd, determine the number of files
50 ;in the lofd (search within subdirectories as well).
52 (define (how-many-lofd a-lofd)
53 (cond
54 [(empty? a-lofd) 0]
55 [(symbol? (first a-lofd)) (+ 1
56 (how-many-lofd (rest a-lofd)))]
57 [(dir? (first a-lofd)) (+ (how-many (first a-lofd))
58 (how-many-lofd (rest a-lofd)))]))