gpt4 book ai didi

LISP 找不到要打开的文件

转载 作者:太空宇宙 更新时间:2023-11-03 18:41:29 24 4
gpt4 key购买 nike

我试图打开一个与我正在运行的 .lsp 文件位于同一文件夹中的文件,但它给我这个错误:错误:没有这样的文件或目录:“a.txt”

这是我使用的代码:

(defun readfile ()
(let (lines columns matrix)
(with-open-file (file "a.txt")
(setq lines (parse-integer (read-char file)))
(setq columns (parse-integer (read-char file))))))

为什么找不到文件?

最佳答案

没有找到,因为你没有说文件在哪里。您所提供的只是一个名称/类型,没有目录。

这个函数的文件在哪里并不重要。它不设置路径名的上下文。

通常情况下,像 Clozure CL 这样的东西会在默认情况下在其启动的目录中查找。

另外 Common Lisp 有一个变量 *default-pathname-defaults*。您可以在那里设置或绑定(bind)路径名的默认值。

您对 CCL 的选择:

  • 在正确的目录中启动 CCL
  • 使用 (:cd "/mydir/foo/bar/") 在 REPL 中设置当前目录。这是 CCL 特有的
  • 设置或绑定(bind)*default-pathname-defaults*

您还可以根据正在加载的源文件计算路径名。您需要在文件中包含以下内容:

(defvar *my-path* *load-pathname*)

(let ((*default-pathname-defaults* (or *my-path*
(error "I have no idea where I am"))))
(readfile))

顺便说一句:Lisp 监听器通常不仅包含“REPL”(Read Eval Print Loop),而且还支持“命令”。 CCL就是这样的情况。要查看 CCL 提供的命令,请使用 :help。在调试器中也有不同的/更多的命令。

Clozure CL 提供查找或设置当前目录的命令非常有用。其他 CL 实现提供类似的功能 - 但方式不同,因为没有针对命令机制(除了 CLIM)和默认命令的标准。

在 Mac 上的 IDE 中运行的 Clozure Common Lisp 示例:

? :help
The following toplevel commands are available:
:KAP Release (but don't reestablish) *LISTENER-AUTORELEASE-POOL*
:SAP Log information about current thread's autorelease-pool(s)
to C's standard error stream
:RAP Release and reestablish *LISTENER-AUTORELEASE-POOL*
:? help
:PWD Print the pathame of the current directory
(:CD DIR) Change to directory DIR (e.g., #p"ccl:" or "/some/dir")
(:PROC &OPTIONAL P) Show information about specified process <p>
/ all processes
(:KILL P) Kill process whose name or ID matches <p>
(:Y &OPTIONAL P) Yield control of terminal-input to process
whose name or ID matches <p>, or to any process if <p> is null
Any other form is evaluated and its results are printed out.

关于LISP 找不到要打开的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16795603/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com