Skip to main content Link Menu Expand (external link) Copy Copied

Lab 3

Partners

Work on this lab with someone different from your homework partner and, if possible, someone you haven’t worked on any other lab with.

The File System

The library htdp/dir provides an abstraction over the file system that we will be working with in this lab. Below are the data definitions provided by htdp/dir:

(require htdp/dir)
; A File is a (make-file String Integer Date String)
; (define-struct file [name size date content])
; - where name is the name of the file (including extension)
; - size is the size of the file in bytes
; - date is the last modified date of the file
; - and content is the contents of the file
 
; A Date is a (make-date Integer Integer Integer Integer Integer Integer)
; (define-struct date [year month day hours minutes seconds])
; - where year is the year the file was modified
; - month is the month the file was modified
; - day is the day the file was modified
; - hours is the hour the file was modified
; - minutes is the minute the file was modified
; - and seconds is the second the file was modified
 
; A Directory is a (make-dir String [List-of Directory] [List-of File])
; (define-struct dir [name dirs files])
; - where name is the directory path
; - dirs is the list of sub-directories in this directory
; - and files is the list of files in this directory
;   (not including the ones in sub-directories)

(define EMPTY-DIR (make-dir "HopesAndDreams" '() '()))
(define CAREER
  (make-dir
   "Career"
   (list (make-dir "CareerApplications"
                   '()
                   (list (make-file "CoverLetter.doc" 31744
                                    (make-date 2015 9 20 11 36 25) "")
                         (make-file "EmploymentApplication.pdf" 231010
                                    (make-date 2015 10 13 13 10 0) "")))
         (make-dir "CareerJob"
                   '()
                   (list (make-file "BackgroundCheck.pdf" 1040138
                                    (make-date 2016 8 23 10 27 10) "")
                         (make-file "I9.pdf" 963654
                                    (make-date 2015 11 20 15 49 45) "")
                         (make-file "JobOffer.pdf" 507887
                                    (make-date 2015 11 20 15 49 0) ""))))
   (list (make-file "References.docx" 11634
                    (make-date 2016 8 6 9 55 15) "")
         (make-file "Resume.doc" 34816
                    (make-date 2016 10 12 13 18 12) "")
         (make-file "Transcript.doc" 140288
                    (make-date 2015 9 11 9 3 0) ""))))
 
; file-temp : File -> ?
(define (file-temp f)
  (... (file-name f) (file-size f) (date-temp (file-date f)) (file-content f)))
 
; date-temp : Date -> ?
(define (date-temp d)
  (... (date-year d) (date-month d) (date-hours d) (date-minutes d) (date-seconds d)))
 
; directory-temp : Directory -> ?
(define (directory-temp d)
  (... (dir-name d) (lod-temp (dir-dirs d)) (lof-temp (dir-files d))))
 
; lod-temp : [List-of Directory] -> ?
(define (lod-temp lod)
  (cond [(empty? lod) ...]
        [(cons? lod) (... (directory-temp (first lod)) (lod-temp (rest lod)))]))
 
; lof-temp : [List-of File] -> ?
(define (lof-temp lof)
  (cond [(empty? lof) ...]
        [(cons? lof) (... (file-temp (first lof)) (lof-temp (rest lof)))]))

Getting started with the filesystem

  1. Design a function that consumes a Directory and a string and determines whether or not a file with that name exists in the directory or any of its subdirectories.

  2. Design a function that consumes a Directory and computes the total size of all files in the whole directory tree. You may assume that directories take up no space.

Switch pair programming roles

  1. Design a function that consumes a Directory d and two Strings src and target, and produces a Directory which has the same files as d but with all files named src renamed to target. Do not rename directories.

  2. Design a function that consumes a Directory d and a Number n and returns a list of file names in the directory tree of d with size at least n.

A lower level view into the filesystem

htdp/dir is doing some special work for us. In a file system, folders and directories aren’t split out into two lists like they are in Directory. Here is a definition that someone dealing more closely with the file system might have to deal with:

; A FSC (FileSystemCompotent) is one of:
; - File
; - Dir
 
; A Dir is a (make-dir2 Symbol [List-of FSC])
(define-struct dir2 [name contents])

Switch pair programming roles

  1. Design the function num-files/dir, which computes the number of files in a Dir (do not just use your dir->directory from the next exercise and num-files). How does the structure of num-files/dir compare to num-files? Is one easier to read and write than the other? Do you think that would be the case for the other functions in the lab? Try rewriting some of the other functions to now work on Dirs.

  2. Design dir->directory, a function which takes a Dir and produces a Directory.

Switch pair programming roles

  1. Design fold-dir, a function which abstracts the two previous problems (and any others that you rewrote to work on Dirs rather than directories).