;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-beginner-abbr-reader.ss" "lang")((modname lec13) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) ;; A directory is ;; - (make-dir string LOFD) (define-struct dir (name content)) ;; A file is ;; - (make-file string num) (define-struct file (name size)) ;; A file-or-directory is either ;; - file ;; - directory ;; A LOFD is either ;; - empty ;; - (cons file-or-directory LOFD) (define escape.rkt (make-file "escape.rkt" 100)) (define kate (make-dir "kate" (list escape.rkt (make-file "map.pdf" 1024)))) (define snacks (make-dir "snacks" (list))) (define lotto.zip (make-file "lotto.zip" 5)) (define hugo (make-dir "hugo" (list snacks lotto.zip))) (define C: (make-dir "C:" (list kate hugo))) ;; find : directory string -> bool ;; To determine whether n exists anywhere starting ;; with dir (define (find dir n) (or (string=? n (dir-name dir)) (lofd-find (dir-content dir) n))) ;; file-find : file string -> bool (define (file-find file n) (string=? (file-name file) n)) ;; fod-find : file-or-directory string -> bool (define (fod-find fod n) (cond [(file? fod) (file-find fod n)] [(dir? fod) (find fod n)])) ;; lofd-find : LOFD string -> bool (define (lofd-find lofd n) (cond [(empty? lofd) false] [(cons? lofd) (or (fod-find (first lofd) n) (lofd-find (rest lofd) n))])) (check-expect (file-find lotto.zip "lotto.zip") true) (check-expect (file-find lotto.zip "escape.rkt") false) (check-expect (fod-find lotto.zip "lotto.zip") true) (check-expect (fod-find lotto.zip "escape.rkt") false) (check-expect (fod-find kate "lotto.zip") false) (check-expect (fod-find hugo "lotto.zip") true) (check-expect (lofd-find empty "lotto.zip") false) (check-expect (lofd-find (list hugo) "lotto.zip") true) (check-expect (lofd-find (list kate (make-file "homework.rkt" 104567)) "lotto.zip") false) (check-expect (find C: "C:") true) (check-expect (find C: "D:") false) (check-expect (find C: "lotto.zip") true) (check-expect (find hugo "C:") false) ;; du : dir -> num (define (du dir) (lofd-du (dir-content dir))) ;; lofd-du : LOFD -> num (define (lofd-du lofd) (cond [(empty? lofd) 0] [(cons? lofd) (+ (fod-du (first lofd)) (lofd-du (rest lofd)))])) ;; fod-du : file-or-directory -> num (define (fod-du fod) (cond [(file? fod) (file-size fod)] [(dir? fod) (du fod)])) (check-expect (du C:) 1129) (check-expect (lofd-du empty) 0) (check-expect (lofd-du (list kate)) 1124) (check-expect (fod-du escape.rkt) 100) (check-expect (fod-du snacks) 0) (check-expect (file-size escape.rkt) 100)