Comp 210 Lab 6: Mutual Recursion, Multiple Complex Arguments

Index: Directory Example, list, Zip Examples

Directory Example

In class we defined the following data definitions:

     A file is a symbol.

     A directory is structure
       (make-dir name contents)
     where name is a symbol, and contents is a l-o-f-d.

     A list-of-files-and-directories (l-o-f-d) is one of
       - empty
       - (cons f lofd)
         where f is a file, and lofd is a l-o-f-d
       - (cons d lofd)
         where d is a directory, and lofd is a l-o-f-d.
and templates:
     ; file-func : file -> ?
     (define (file-func a-file)

     ; dir-func : directory -> ?
     (define (dir-func a-dir)
        ...(dir-name a-dir)...
        ...(lofd-func (dir-contents a-dir))...)

     ; lofd-func : l-o-f-d -> ?
     (define (lofd-func a-lofd)
           [(empty? a-lofd)        ...]
           [(file? (first a-lofd)) ...(file-func (first a-lofd))...
                                   ...(lofd-func (rest a-lofd))...]
           [(dir? (first a-lofd))  ...(dir-func (first a-lofd))...
                                   ...(lofd-func (rest a-lofd))...]))
and a useful predicate:
     (define (file? any)
        (symbol? any))

To do: Write a function

     ; flatten-dir : directory symbol -> (directory or l-o-f-d)
     ; Returns a structure like the original directory, except that the
     ; named directory is removed, with its contents moved up one level.
Here are a couple pictorial examples, in both cases removing the directory named to-remove.
Input Output
Example 1:
   /  |  \
bar baz to-remove
         / \
       one two
   /  /  \  \
bar baz one two
Example 2:
  /  |  \
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.

Here is a solution.


The following is a convenient abbreviation:

     (list 1 3 5 7)
     (cons 1 (cons 3 (cons 5 (cons 7 empty))))
Using list introduces the exact same cons structure, but it is easier for humans to read in big lists. When writing programs on lists, you should still think in terms of the cons structure.

In short, cons and empty are the constructors of lists. list is another function which makes lists, and is convenient shorthand for combinations of the constructors.

Zip Examples

To do:

Could you also write zip3 and unzip3, etc.?