String-append to files in a directory with Scheme

Please note, first of all, that this is a homework question, so I am not looking for a direct code or something like that, just so that someone can help me with my logic.

Assignment is performed in DrRacket. The question asks the question:

Given the file system, which we defined as a structure with two fields, the name and content, where the content is a list of either directories or files; write a function that will create the file name ".bak" for each file in the directory and place it immediately after the file.

I am completely lost. My logic is this: if the first in the content list is a file, just redo the directory with that file and add a new file with the addition of ".bak". This is as far as I can get - I can’t understand how to work if there is a subdirectory, or how to move further on the list.

Here is my awful code:

(define (backup my-fs) (cond [(empty? (dir-contents my-fs)) empty] [(file? (first (dir-contents my-fs))) (make-dir (dir-name my-fs) (append (backup-list (first (dir-contents my-fs)))(rest (dir-contents my-fs))))] [(dir? (first (dir-contents my-fs))) (backup (first (dir-contents my-fs)))])) 

Can someone help me explain this?

+4
source share
2 answers

The contents part of your FileSystem is a list containing files or directories (which contain lists containing ....).

This is a basic tree traversal problem in which you have three cases, as you noted:

  • the list is empty.
  • The first item in the list is the file
  • The first item in the list is the directory.

Then you need an action for each case:

  • done
  • save this file name, create a new file name and continue processing the rest of the list
  • save this directory by processing it and continue processing the rest of the list.

For instance:

 (define (traverse contents) (cond [(empty? contents) ... nothing to do ...] [(file? (first contents)) ;; if the first element a file: (cons (first contents) ;; keep the file (cons (... make backup filename ... (first contents)) ;; make the backup (traverse (rest contents))))] ;; and recurse on the rest [(dir? (first contents) ;; if the first element a directory: (cons (traverse (first contents)) ;; recurse on the first (traverse (rest contents)))])) ;; and also recurse on the rest 
+2
source

You need to clarify your data definition. You write:

"Given the file system that we defined as a structure with two fields, a name and content, where the content is a list of directories or files, write a function that will create a file name" .bak "for each file in the directory and place it immediately after the file . "

This makes it clear what FileSystem is ... if you know what "directories" and "files" are. You need to clarify this by writing data definitions for "directory" and "file". Each of them should be a separate sentence. They can be very simple, for example. "The file is presented as a string."

After that write some examples of FileSystems.

+1
source

Source: https://habr.com/ru/post/1380242/


All Articles