1

例如,在我的main.scm文件中,我有(load "util.scm"). util.scm是与 . 位于同一文件夹中的文件main.scm。这两个文件都位于~/documents/myproject/.

现在,当我在这个目录中时,我运行$ chez-scheme main.scm一切正常。但是,如果我在我的主目录中运行$chez-scheme documents/myproject/main.scm它会抱怨,找不到文件util.scm。我想是这种情况,因为当前目录是我相关的主目录,因此util.scm确实不存在,它实际上在documents/myproject/. 话虽这么说,我已经习惯(在其他语言中)相对于包含导入指令的文件查找这些路径的功能,我也想在这里拥有它。我已经尝试通过将文件添加前缀./或将文件定义为库并执行(import (util))但它在documents/myproject/. 有什么办法可以让它按我的意图工作吗?

我认为这是特定于 Chez-Scheme 的。如果不是,我更喜欢实现中立的答案。

4

2 回答 2

1

load在 R5RS 中有点尴尬,因为报告指出系统接口在报告中是题外话,但它们包括load了一个半心半意的解决方案。该报告没有说明它load是相对于当前目录还是load表单源自的文件,所以为了便于移植,我猜您需要从当前目录运行脚本并让您加载的文件相对于两者。

由于 Chez Scheme 实现了 R6RSload并不是真正正确的使用形式。删除 R6RSload以支持库。您应该使您的文件成为一个库并咨询如何安装它。在某些系统中,只是将文件放置在正确的路径中,在配置中添加库位置或运行安装脚本。通过使用import.

根据Chez 文档,您可以传递--libdirs给它一个或多个路径来考虑加载库。您可以通过评估来查看它扫描的路径(library-directories)

于 2016-10-22T00:33:57.500 回答
0

There are several different ways to accomplish what (I think) you are trying to do, but eventually they all boil down to letting Chez know where to look for things. When given relative paths, include and load use the source-directories parameter to search for the requested file. Libraries have their path automatically prepended to source-directories while they are being loaded or compiled, so if your main.scm were a library definition then it would find util.scm as you expect.

However, it sounds like main.scm isn't a library, it's a top-level program. Unfortunately, Chez doesn't have a command line option to set the source-directories like it does for library directories. That leaves you with a bit less flexibility. Any of the following will work:

  • Make util.scm a library and invoke Chez with the --libdirs option to let it know where to look for libraries.
  • Set source-directories and load main.scm from inside the REPL rather than from the command line.
  • Write a wrapper shell script that does the above by echoing the commands into scheme so you don't have to type it yourself. (Only suitable if you don't also need to then type into the scheme session).
  • Write a wrapper shell script that cds into your project directory before running scheme (and presumably cds back to the original directory when it's done).
于 2016-10-22T15:25:24.810 回答