使用Scheme将字符串附加到目录中的文件

发布于 2024-12-14 03:12:08 字数 717 浏览 1 评论 0原文

首先请注意,这是一个家庭作业问题,所以我不是在寻找直接代码或类似的东西,只是为了有人可以帮助我解决我的逻辑。

作业在 DrRacket 中。问题问:

给定一个文件系统,我们将其定义为具有两个字段的结构:名称和内容,其中内容是目录或文件的列表;编写一个函数,为目录中的每个文件创建一个“.bak”文件名,并将其放置在该文件之后。

我完全迷路了。我的逻辑如下:如果内容列表中的第一个内容是一个文件,只需使用该文件重新创建目录,并添加一个附加“.bak”的新文件即可。这是我所能得到的——如果有子目录,我不知道如何解决问题,或者如何进一步向下移动列表。

这是我的糟糕代码:

(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)))]))

任何人都可以帮我解释这个问题吗?

Please note first of all that this is a homework question so I'm not looking for straight code or anything like that, just for someone to maybe help me with my logic.

The assignment is in DrRacket. The question asks:

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

I am totally lost. My logic is as follows: If the first thing in the content list is a file, simply remake the directory with that file and a new file with ".bak" appended. This is as far as I can get - I can't see how to work things out if there's a subdirectory, OR how to go about moving further down the list.

Here's my atrocious 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 anyone help me reason this out?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纵情客 2024-12-21 03:12:08

FileSystemcontents 部分是包含文件或目录的列表(这些列表包含...)。

这是一个基本的树遍历问题,其中有三种情况,正如您所指出的:

  1. 列表为空
  2. 列表中的第一个元素是文件
  3. 列表中的第一个元素是目录

那么您需要针对每种情况执行一个操作:

  1. 完成
  2. 保留该文件名,创建一个新的文件名,并继续处理列表的其余部分
  3. 保留该目录,对其进行递归,然后继续处理列表的其余部分

例如:

(define (traverse contents)
  (cond
    [(empty? contents) ... nothing to do ...]
    [(file? (first contents))              ;; if the first element's 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's a directory:
      (cons (traverse (first contents))    ;;   recurse on the first
            (traverse (rest contents)))])) ;;   and also recurse on the rest

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

This is a basic tree-traversal problem where you have three cases, as you noted:

  1. list is empty
  2. first element in list is a file
  3. first element in list is a directory

Then you need an action for each case:

  1. done
  2. keep that filename, create a new filename, and continue processing the rest of the list
  3. keep that directory, recursing over it, and continue processing the rest of the list

For example:

(define (traverse contents)
  (cond
    [(empty? contents) ... nothing to do ...]
    [(file? (first contents))              ;; if the first element's 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's a directory:
      (cons (traverse (first contents))    ;;   recurse on the first
            (traverse (rest contents)))])) ;;   and also recurse on the rest
叫嚣ゝ 2024-12-21 03:12:08

您需要澄清您的数据定义。您可以这样写:

“给定一个文件系统,我们将其定义为具有两个字段(名称和内容)的结构,其中内容是目录或文件的列表;编写一个函数,为每个文件创建一个“.bak”文件名放在目录中并将其紧接在文件后面。”

如果您知道“目录”和“文件”是什么,这可以清楚地说明什么是文件系统。您需要通过编写“目录”和“文件”的数据定义来澄清这一点。其中每一个都应该是一个单独的句子。它们可能非常简单,例如“文件表示为字符串”。

完成此操作后,编写一些文件系统的示例。

You need to clarify your data definition. You write:

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

This makes it clear what a 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 these should be a separate sentence. They might be really simple, e.g. "A file is represented as a string".

After doing this, write some examples of FileSystems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文