Refining Functions and Programs

The goal of the following sequence of exercises is to develop several common utility functions for directory and file systems, using our third and most refined model. Even though these functions process Scheme-based representation of files and directories, they give us a good idea how such real-world functions work. <#19213#>Exercise 16.3.1<#19213#> Translate the file system in figure~#figaccountant#19215> into a Scheme representation. Remember to use <#64028#><#19216#>empty<#19216#><#64028#> for the content of the files.~ external Solution<#64029#><#64029#> To make the exercise more realistic, DrScheme supports the teachpack <#19222#>dir.ss<#19222#>. It introduces the two necessary structure definitions and a function to create representations of directories according to our third model:
<#71120#>;; <#64030#><#19227#>create-dir<#19227#> <#19228#>:<#19228#> <#19229#>string<#19229#> <#19230#><#19230#><#19231#>-;SPMgt;<#19231#><#19232#><#19232#> <#19233#>dir<#19233#><#64030#><#71120#>
<#71121#>;; to create a representation of the directory that <#64031#><#19234#>a-path<#19234#><#64031#> specifies:<#71121#> 
<#71122#>;; 1. Windows: <#64032#><#19235#>(create-dir<#19235#> <#19236#>``c:\<#19236#><#19237#>\<#19237#><#19238#>windows'')<#19238#><#64032#> <#71122#> 
<#71123#>;; 2. Mac: <#64033#><#19239#>(create-dir<#19239#> <#19240#>``My<#19240#> <#19241#>Disk:'')<#19241#><#64033#> <#71123#> 
<#71124#>;; 3. Unix: <#64034#><#19242#>(create-dir<#19242#> <#19243#>``/<#19243#><#19244#>home/<#19244#><#19245#>scheme/<#19245#><#19246#>'')<#19246#><#64034#><#71124#> 
<#19247#>(define<#19247#> <#19248#>(create-dir<#19248#> <#19249#>a-path)<#19249#> <#19250#>...)<#19250#> 
Use the function to create some small and large examples based on the directories in a real computer. <#19254#>Warning<#19254#>: For large directory trees, DrScheme may need a lot of time to build a representation. Use <#64035#><#19255#>create-dir<#19255#><#64035#> on small directory trees first. <#19256#>Exercise 16.3.2<#19256#> Develop the function <#64036#><#19258#>how-many<#19258#><#64036#>, which consumes a <#64037#><#19259#>dir<#19259#><#64037#> (according to model~3) and produces the number of files in the <#64038#><#19260#>dir<#19260#><#64038#> tree. Test the function on the directories created in exercise~#exfileexample3#19261>. Why are we confident that the function produces correct results?~ external Solution<#64039#><#64039#> <#19267#>Exercise 16.3.3<#19267#> Develop the function <#64040#><#19269#>du-dir<#19269#><#64040#>. The function consumes a directory and computes the total size of all the files in the entire directory tree. This function approximates a true disk-usage meter in that it assumes that directory don't require storage. Refine the function to compute approximate sizes for subdirectories. Let's assume that storing a file and a directory in the <#64041#><#19270#>content<#19270#><#64041#> field costs <#64042#><#19271#>1<#19271#><#64042#> storage unit.~ external Solution<#64043#><#64043#> <#19277#>Exercise 16.3.4<#19277#> Develop the function <#64044#><#19279#>find?<#19279#><#64044#>, which consumes a <#64045#><#19280#>dir<#19280#><#64045#> and a file name and determines whether or not a file with this name occurs in the directory tree. <#19281#>Challenge<#19281#>: Develop the function <#64046#><#19282#>find<#19282#><#64046#>. It consumes a directory <#64047#><#19283#>d<#19283#><#64047#> and a file name <#64048#><#19284#>f<#19284#><#64048#>. If <#64049#><#19285#>(find?<#19285#>\ <#19286#>d<#19286#>\ <#19287#>f)<#19287#><#64049#> is true, it produces a path to the file; otherwise it produces <#64050#><#19288#>false<#19288#><#64050#>. A path is a list of directory names. The first one is that of the given directory; the last one is that of the subdirectory whose <#64051#><#19289#>files<#19289#><#64051#> list contains <#64052#><#19290#>f<#19290#><#64052#>. For example:
  <#19295#>(find<#19295#> <#19296#>TS<#19296#> <#19297#>'<#19297#><#19298#>part3)<#19298#>
<#19299#>=<#19299#> <#19300#>(list<#19300#> <#19301#>'<#19301#><#19302#>TS<#19302#> <#19303#>'<#19303#><#19304#>Text)<#19304#> 
  <#19312#>(find<#19312#> <#19313#>TS<#19313#> <#19314#>'<#19314#><#19315#>read!)<#19315#>
<#19316#>=<#19316#> <#19317#>(list<#19317#> <#19318#>'<#19318#><#19319#>TS)<#19319#> 
assuming <#64053#><#19323#>TS<#19323#><#64053#> is <#64054#><#19324#>define<#19324#><#64054#>d to be the directory in figure~#figaccountant#19325>. Which <#19326#>read!<#19326#>\ file in figure~#figaccountant#19327> should <#64055#><#19328#>find<#19328#><#64055#> discover? Generalize the function to return a list of paths if the file name occurs more than once. Each path should lead to a different occurrence, and there should be a path for each occurrence.~ external Solution<#64056#><#64056#>