Created
February 3, 2011 09:27
-
-
Save twfarland/809257 to your computer and use it in GitHub Desktop.
My solution for exercise 16.3 extended challenge in How to Design Programs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
;a file is a structure (make-file n s x) where n is a symbol, s is a number, and x is some scheme value | |
(define-struct file (name size content)) | |
;a dir is a structure (make-dir n ds fs) where n is a symbol and ds is a list of directories and fs is a list of files | |
(define-struct dir (name dirs files)) | |
; a list of files is either empty or (cons f lof) where f is a file and lof is a list of files | |
; a list of dirs is either empty or (cons d lod) where d is a dir and lod is a list of dirs | |
(define nfs | |
(make-dir 'TS | |
(list (make-dir 'Text | |
empty | |
(list (make-file 'part1 99 empty) | |
(make-file 'part2 52 empty) | |
(make-file 'part3 17 empty))) | |
(make-dir 'Libs | |
(list (make-dir 'Code | |
empty | |
(list (make-file 'hang 8 empty) | |
(make-file 'draw 2 empty))) | |
(make-dir 'Docs | |
empty | |
(list (make-file 'read! 19 empty)))) | |
empty)) | |
(list (make-file 'read! 10 empty)))) | |
(define (fileInList? filename filelist) | |
(cond | |
[(empty? filelist) false] | |
[else (or (symbol=? filename (file-name (first filelist))) | |
(fileInList? filename (rest filelist)))])) | |
(define (pathsInDir filename dir [lineage empty]) | |
(let ([pathsInChildren (pathsInDirList filename | |
(dir-dirs dir) | |
(append lineage (list (dir-name dir))))]) | |
(cond | |
[(fileInList? filename (dir-files dir)) | |
(cons (append lineage (list (dir-name dir))) | |
pathsInChildren)] | |
[else pathsInChildren]))) | |
(define (pathsInDirList filename dirlist lineage) | |
(cond | |
[(empty? dirlist) empty] | |
[else (append (pathsInDir filename (first dirlist) lineage) | |
(pathsInDirList filename (rest dirlist) lineage))])) | |
(pathsInDir 'read! nfs) | |
(pathsInDir 'part3 nfs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment