Remember:
- Work in pairs
- Change roles often
- Follow the design recipe for every problem
As computer users we can create files and directories (sometimes called folders) on our computers to store our data and our programs. It is useful to have operations that allow manipulating these files and directories. Consider the following data definition of a flat FileSystem (FS), i.e., no directories, just files.
;; A File is a String ;; An FS (FileSystem) is one of ;; -- empty ;; -- (cons File FS)Exercise 1:
Design a function calledtotalthat consumes a FileSystemfsand produces the total number of files infs.Exercise 2:
Design a function that consumes a FileSystem and a File and returnstrueif the file is present in the FileSystem andfalseotherwise.
Flat FileSystems quickly become chaotic. We would instead like to have a hierarchical way to structure our files on our machines, so we extend our previous data definition to allow directories as well.
;; A File is a String ;; A DirEntry is one of: ;; -- File ;; -- Dir ;; A Dir is one of ;; -- empty ;; -- (cons DirEntry Dir) ;; ;; Interpretation: A directory is constructed by adding directory entries, which ;; may be files or directories, to the empty directory. ;; - (cons f d) is the directory containing the file f and all the ;; entries of directory d ;; - (cons d1 d2) creates the directory containing the subdirectory d1 and all ;; the entries of directory d2Exercise 3:
Construct a Dir that represents the following directory tree. Note that our simple data definitions don't allow names for directories.- hw3.ss - hw1.ss + - lists-and-recursion.ss - structs.ss - tetris.ss - syllabus.pdf + - olin.png - david.png - r6rs.pdf - kitteh.png - hw2.ssExercise 4:
Design a function that consumes a Dirdand produces the total number of files in the whole directory tree ofd.Exercise 5:
Design a function that consumes a Dirdand a Filefand returnstrueiffexists indor any of its subdirectories andfalseotherwise.Exercise 6:
Design a function that consumes a Dirdand two Filessrcandtarget, and produces a Dir which has the same files asdbut with all files namedsrcrenamed totarget.
Directories gave us more structure but the information that we have for each file and directory is still minimal. Let's add attributes to files and directories so we can track who owns a file and what its size is on disk:
(define-struct file (name size owner)) ;; A File is (make-file String Number String) ;; interp. (make-file s n o) creates a File with name s, size n kilobytes, ;; and owned by o.Let's also add names to directories and split their contents into two separate pieces, a list of subdirectories and a list of files:
;; An LoF is one of ;; -- empty ;; -- (cons File LoF) (define-struct dir (name dirs files)) ;; A Dir is a (make-dir String LoD LoF) ;; interp. (make-dir s ds fs) creates a Dir with name s containing ;; ds as its list of subdirectories and fs as its list of files. ;; An LoD is one of ;; -- empty ;; -- (cons Dir LoD)Exercise 7:
Design a function that consumes a Dirdand produces the total number of files (just files, not directories) in the directory tree ofd.Exercise 8:
Design a function that consumes a Dirdand a stringsrepresenting an owner and returns a list of files owned bysin the directory tree ofd.Exercise 9:
Design a function that consumes a Dirdand a Numbernrepresenting file size and returns a list of files in the directory tree ofdwith size at leastn.Exercise 10:
Design a function that consumes a Dirdand computes the total size of all files in the whole directory tree. You may assume that directories take up no space.If you finish early, try designing a function that draws the contents of a directory tree as an image…
Enjoy!