;; 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 |37.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"))))) ;A directory (dir) is a structure ;(make-dir name content size systems) where name, systems are symbols, ;size is a number, and content is a list-of-files-and-directories (LOFD). ; ;A list-of-files-and-directories (LOFD) is either ;1. empty, ;2. (cons file lofd) ;3. (cons dir lofd), ;where file is a symbol, dir is a directory, ;and lofd is a LOFD (list-of-files-and-directories). (define-struct dir (name content size systems)) ;Template ;fun-for-dir : dir -> ??? ;(define (fun-for-dir a-dir) ; (... (dir-name a-dir) ... ; ... (fun-for-lofd (dir-content a-dir)) ...)) ; ; ;fun-for-lofd : lofd -> ??? ;(define (fun-for-lofd a-lofd) ; (cond ; [(empty? a-lofd) ...] ; [(symbol? (first a-lofd)) ... (first a-lofd) ... ; ... (fun-for-lofd (rest a-lofd)) ...] ; [(dir? (first a-lofd)) ... (fun-for-dir (first a-lofd)) ... ; ... (fun-for-lofd (rest a-lofd)) ...])) ;Examples: (define Code (make-dir 'Code '(hang draw) 5 'ReadOnly)) (define Docs (make-dir 'Docs '(read!) 5 'ReadWrite)) (define Libs (make-dir 'Libs (list Code Docs) 5 'Executable)) (define Text (make-dir 'Text '(part1 part2 part3) 5 'ReadOnly)) (define TS (make-dir 'TS (list Text Libs 'read!) 5 'None)) ;how-many : dir -> number ;Given a-dir, determine the number of ;files in the dir tree. (define (how-many a-dir) (how-many-lofd (dir-content a-dir))) ;how-many-lofd : lofd -> number ;Given a-lofd, determine the number of files ;in the lofd (search within subdirectories as well). (define (how-many-lofd a-lofd) (cond [(empty? a-lofd) 0] [(symbol? (first a-lofd)) (+ 1 (how-many-lofd (rest a-lofd)))] [(dir? (first a-lofd)) (+ (how-many (first a-lofd)) (how-many-lofd (rest a-lofd)))]))