;; Teachpack: dir.ss #| Commented out because these structure definitions are part of dir.ss (define-struct file (name size content)) (define-struct dir (name dirs files)) |# ;; A File is (make-file Symbol Number empty) ;; A Dir is (make-dir Symbol Dir-List File-List) ;; A File-List is one of ;; - empty ;; - (cons File File-List) ;; A Dir-List is one of ;; - empty ;; - (cons Dir Dir-List) ;;;; Main Program ;; count-files-in-directory : Dir -> Number ;; counts how many files in this directory (define (count-files-in-directory d) (+ (count-files-in-file-list (dir-files d)) (count-files-in-directory-list (dir-dirs d)))) ;;;; Auxiliaries ;; count-files-in-file-list : File-List -> Number ;; counts how many files in this file list (define (count-files-in-file-list fl) (cond [(empty? fl) 0] [else (+ (count-files-in-file (first fl)) (count-files-in-file-list (rest fl)))])) ;; count-files-in-directory-list : Dir-List -> Number ;; counts how many files in this directory list (define (count-files-in-directory-list dl) (cond [(empty? dl) 0] [else (+ (count-files-in-directory (first dl)) (count-files-in-directory-list (rest dl)))])) ;; count-files-in-file : File -> Number ;; counts how many files in this file (define (count-files-in-file f) 1) ;;;; Tests ;; Tests of create-dir (part of dir.ss) (create-dir ".") ;(create-dir "C:\\Program Files") ;; takes a long time to complete! ;; Tests of count-files-in-directory (count-files-in-directory (create-dir ".")) ;(count-files-in-directory (create-dir "C:\\Program Files"))