[racket] missing solution 16.3.3 ex:file-du
A candidate for a solution.
;; model 3
(define-struct file (name size content))
(define-struct dir (name dirs files))
;; files:
(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))
;; directories:
(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)))
;; dur-dir :: dir -> number
;; consumes a directory producing a sum of the list of files in each.
;; to the sum, it'll add 1 for each directory it finds. (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))))
(define (du-dir-subdir ls)
(cond
[(empty? ls) 0]
[(dir? (first ls))
(+ (du-dir (first ls))
(du-dir-subdir (rest ls)))]))
;; sum-files :: ls-of-files -> number
;; consumes a list of files producing the sum of their 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))
Source:
> http://www.htdp.org/2003-09-26/Solutions/file-du.html
> No Solution Written, yetUnfortunately, the solution to this exercise has
> not yet been written. To submit a solution you have written to this
> problem, or to complain that the solution isn't available, please contact Robby
> Findler
> <robby at eecs.northwestern.edu?subject=missing%20solution%2016.3.3%20ex:file-du%20>
> .
>
> To see the list of solutions, visit the table of contents
> <http://www.htdp.org/2003-09-26/Solutions/contents.html>. Each of the
> hyperlinked exercise numbers has a solution.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140808/5a2cf6b7/attachment.html>