[racket] missing solution 16.3.3 ex:file-du
Robby, let's go with this one. -- Matthias
;; credit to Daniel Bastos
;; model 3 structure definitions [also available from teachpack]
;; (define-struct file (name size content))
;; (define-struct dir (name dirs files))
;; data definitions as in book
(require htdp/dir)
;; data examples
(define hang (make-file 'hang 8 empty))
(define draw (make-file 'draw 2 empty))
(define read (make-file 'read! 19 empty))
(define one (make-file 'part1 99 empty))
(define two (make-file 'part2 52 empty))
(define thre (make-file 'part3 17 empty))
(define rdme (make-file 'read 10 empty))
(define Code (make-dir 'Code '() (list hang draw)))
(define Docs (make-dir 'Docs '() (list read)))
(define Libs (make-dir 'Libs (list Code Docs) '()))
(define Text (make-dir 'Text '() (list one two thre)))
(define Top (make-dir 'TS (list Text Libs) (list rdme)))
;; dir -> number
;; determine the total size of the files in this directory
;; (we're assuming each directory is of size 1.)
(define (du-dir d)
(+ 1 (sum-files (dir-files d)) (du-dir-subdir (dir-dirs d))))
;; list-of-directories -> number
;; determine the total size of the files in a list of directories
(define (du-dir-subdir ls)
(cond
[(empty? ls) 0]
[(dir? (first ls)) (+ (du-dir (first ls)) (du-dir-subdir (rest ls)))]))
;; sum-files :: list-of-files -> number
;; produces the sum of the file's sizes
(define (sum-files ls)
(cond
[(empty? ls) 0]
[else (+ (file-size (first ls)) (sum-files (rest ls)))]))
;; tests
(check-expect (sum-files (list hang draw)) 10)
(check-expect (sum-files empty) 0)
(check-expect (du-dir Libs) (+ 8 2 19 1 1 1))