使用Scheme将字符串附加到目录中的文件
首先请注意,这是一个家庭作业问题,所以我不是在寻找直接代码或类似的东西,只是为了有人可以帮助我解决我的逻辑。
作业在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
FileSystem
的contents
部分是包含文件或目录的列表(这些列表包含...)。这是一个基本的树遍历问题,其中有三种情况,正如您所指出的:
那么您需要针对每种情况执行一个操作:
例如:
The
contents
part of yourFileSystem
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:
Then you need an action for each case:
For example:
您需要澄清您的数据定义。您可以这样写:
“给定一个文件系统,我们将其定义为具有两个字段(名称和内容)的结构,其中内容是目录或文件的列表;编写一个函数,为每个文件创建一个“.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.