If you have not already done so,
please take the quiz before continuing with lab.
If you have not already done so,
please go over last week's lab, first.
Instructions for students & labbies:
Students use DrScheme, following the design recipe,
on the exercises at their own pace,
while labbies wander among the students, answering questions,
bringing the more important ones to the lab's attention.
Students should feel free to skip the challenge exercises.
Files and Directories
The following are data definitions for one possible (simplified) representation
of files and directories (a.k.a. folders).
Observe the mutual recursion between files and list-of-files.
(define-struct dir (name contents))
; A file is one of
; - a symbol
; representing a "simple" file's name
; - a directory
; (make-dir name contents)
; where name is a symbol, and contents is a l-o-f.
; A list-of-files (l-o-f) is one of
; - empty
; - (cons f lofd)
; where f is a file, and lofd is a l-o-f
This is very similar to the descendant trees data structure seen in class.
Tree-based data structures are very common!
Directory exercises
-
Create some sample data for the above types.
-
Write the templates for the above types.
-
Develop a function
; find? : symbol file -> boolean
; Returns whether the filename is anywhere in the
; tree of files represented by the file. This includes both
; simple file names and directory names.
Aside
Note that this is a vast simplification of find, the
mother-of-all everything-but-the-kitchen-sink UNIX directory
traversing command. If you're curious, at a UNIX prompt, enter
man find to see what it can do.
|
-
Use DrScheme's stepper to step through an example use
of
find?.
Following the templates leads to an overall strategy known as
depth-first search. I.e., it explores each tree branch to the
end before moving on to the next branch.
-
Develop the following function:
; any-duplicate-names? : file -> boolean
; Returns whether any (sub)directory directly or indirectly contains
; another directory or file of the same name. It does NOT check
; for duplicated names in separate branches of the tree.
There's a straightforward way that just follows the template.
-
Challenge:
Develop a program to check for duplicated names among
all directories and files in the given tree, not just
subdirectories.
Here's a hint.
-
Develop the following function:
; flatten-dir-once : symbol file -> (file or l-o-f)
; Returns a structure like the original file, except that any
; (sub)directory with that name is removed and its contents
; moved up one level.
Here are two pictorial examples, in both cases removing the directory
named to-remove. These illustrate why this function can
return either a file or a list of files.
|
Input |
Output |
| Example 1: |
foo
/ | \
bar baz to-remove
/ \
one two
|
foo
/ / \ \
bar baz one two
|
| Example 2: |
to-remove
/ | \
foo bar baz
|
foo bar baz
|
Follow the templates and think about a single
case at a time.
If you do that, it shouldn't be too difficult. If you don't, you'll
probably have real trouble.
|
|
Sample solutions.
|