I'm trying to write a function find which consumes a directory tree and a file name and determines whether or not a file with that name occurs in the directory tree. What i've written work with files within the first level of the tree (read!), but does not "see" the files further in. The only way to know whether the file is not in the tree is if every directory and file is checked, but some directories do not have files and others don't have directories, but some do. What i've written do far returns false for files I know are in the tree. I don't know how to write the false condition.
(define-struct dir (name dirs files))
(define-struct file (name size content))
(define fs (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)))) (list)))
(list (make-file 'read! 10 empty))))
(define (find fs name)
(cond
[(not (empty? (dir-dirs fs)))
(dp (dir-dirs fs) name)]
[(not (empty? (dir-files fs)))
(fc (dir-files fs) name)]
[else false]))
(define (dp dirs name) ;consumes lists of directories
(cond
[(empty? dirs) false] ;I don't know what else to put here but false
[else (find (first dirs) name)
(dp (rest dirs) name)]))
(define (fc files name) ;consumes lists of files
(cond
[(empty? files) false]
[(symbol=? name (file-name (first files))) true]
[else (fc (rest files) name)]))
Here is the solution, (see comments within the code):
Data Definition
Examples
Template
Tests
Functions
If we use this data definition for Dir, then we can use list abstractions:
We can also use the power of local to remove the filename parameter
In fact, further simplifications can be made: