为什么我不能在 Common Lisp 中读取这个文件?
Why I can't read this file in Common Lisp?
我正在尝试使用 Common Lisp with-open-file
读取文件。在我的环境中,我使用 SBCL、Emacs 和 Slime。
我可以使用以下方式打开 this one(书签到 read.lisp)之类的文件:
(defun read-my-file ()
(with-open-file
(stream "~/miscellaneous/misc/symbolic-computation/bookmarks-to-read.lisp")
(let ((contents
(read-all-objects stream (list '$eof$))))
(format t "~&Read ~S objects from the file."
(length contents))
contents)))
(defun read-all-objects (stream eof-indicator)
(let ((result (read stream nil eof-indicator)))
(if (eq result eof-indicator)
nil
(cons result (read-all-objects stream eof-indicator)))))
在 REPL 上调用函数后,我得到了预期的内容:
CL-USER> (read-my-file)
Read 1 objects from the file.
(((:URL "https://calendar.google.com/calendar/u/0/r/week?pli=1" :TITLE
.
.
.
(:URL "https://www.rescuetime.com/dashboard" :TITLE
"RescueTime - Your Daily dashboard" :DATE
"2021-05-16T23:08:46.605691Z" :TAGS ("rescue"))))
注意这是 .lisp
。但是,如果我用这个 other file (history-to-read.lisp) 尝试同样的事情,我会得到一个错误。
我只需要更改位置:
(defun read-my-file ()
(with-open-file
(stream "~/miscellaneous/misc/symbolic-computation/history-to-read.lisp")
(let ((contents
(read-all-objects stream (list '$eof$))))
(format t "~&Read ~S objects from the file."
(length contents))
contents)))
(defun read-all-objects (stream eof-indicator)
(let ((result (read stream nil eof-indicator)))
(if (eq result eof-indicator)
nil
(cons result (read-all-objects stream eof-indicator))))
然后,在 REPL 上调用函数后:
CL-USER> (read-my-file) ; Evaluation aborted on
#<SB-INT:SIMPLE-READER-PACKAGE-ERROR "Package ~A does not exist." {1003AB9273}>.
我收到此错误消息:
Package HISTORY-TREE does not exist.
Line: 3, Column: 45, File-Position: 110
Stream: #<SB-SYS:FD-STREAM for "file
/home/pedro/miscellaneous/misc/symbolic-computation/history-to-read.lisp"
{1003AB83A3}> [Condition of type
SB-INT:SIMPLE-READER-PACKAGE-ERROR]
“历史树”只是写在文字上的东西。喜欢 foo.
此外,我尝试重现@RainerJoswig 描述的建议。不幸的是,同样的问题发生了。
1 - 因为两个文件都是 .lisp
并且读取它们的函数是相同的,为什么会这样?
2 - 为什么文本的 meaning/content 在这里很重要?
3 - 我该如何阅读这个文件?
Package HISTORY-TREE does not exist
该消息很明确:包 HISTORY-TREE
不存在。但是您尝试将符号读入这个不存在的包中,例如 HISTORY-TREE:OWNER
.
该文件中打印符号中提到的每个包都需要存在,才能成功读取该文件。
默认的 Common Lisp reader 不会仅通过读取符号来即时创建包。
示例:
CL-USER 130 > (read-from-string "bar::foo")
Error: Reader cannot find package BAR.
1 (continue) Create the BAR package.
2 Use another package instead of BAR.
3 Try finding package BAR again.
4 (abort) Return to top loop level 0.
Type :b for backtrace or :c <option number> to proceed.
Type :bug-form "<subject>" for a bug report template or :? for other options.
现在让我们使用错误重启来创建该包:
CL-USER 131 : 1 > :c 1
BAR::FOO
8
替代解决方案:
- 在读取数据之前创建包
- 不要打印带有包前缀的符号
- 使用自定义 reader,它可以创建包或将符号标记为具有未定义的包
备注
(defun read-all-objects (stream eof-indicator)
(let ((result (read stream nil eof-indicator)))
(if (eq result eof-indicator)
nil
(cons result (read-all-objects stream eof-indicator))))
例如可以这样写:
(defun read-all-objects (stream &aux (eof '#:eof))
(loop for item = (read stream nil eof)
until (eq item eof)
collect item))
好处:
- 这里不需要 EOF 参数,这里我们使用一个 uninterned 符号
- 列表项的数量不受堆栈深度的限制
我正在尝试使用 Common Lisp with-open-file
读取文件。在我的环境中,我使用 SBCL、Emacs 和 Slime。
我可以使用以下方式打开 this one(书签到 read.lisp)之类的文件:
(defun read-my-file ()
(with-open-file
(stream "~/miscellaneous/misc/symbolic-computation/bookmarks-to-read.lisp")
(let ((contents
(read-all-objects stream (list '$eof$))))
(format t "~&Read ~S objects from the file."
(length contents))
contents)))
(defun read-all-objects (stream eof-indicator)
(let ((result (read stream nil eof-indicator)))
(if (eq result eof-indicator)
nil
(cons result (read-all-objects stream eof-indicator)))))
在 REPL 上调用函数后,我得到了预期的内容:
CL-USER> (read-my-file)
Read 1 objects from the file.
(((:URL "https://calendar.google.com/calendar/u/0/r/week?pli=1" :TITLE
.
.
.
(:URL "https://www.rescuetime.com/dashboard" :TITLE
"RescueTime - Your Daily dashboard" :DATE
"2021-05-16T23:08:46.605691Z" :TAGS ("rescue"))))
注意这是 .lisp
。但是,如果我用这个 other file (history-to-read.lisp) 尝试同样的事情,我会得到一个错误。
我只需要更改位置:
(defun read-my-file ()
(with-open-file
(stream "~/miscellaneous/misc/symbolic-computation/history-to-read.lisp")
(let ((contents
(read-all-objects stream (list '$eof$))))
(format t "~&Read ~S objects from the file."
(length contents))
contents)))
(defun read-all-objects (stream eof-indicator)
(let ((result (read stream nil eof-indicator)))
(if (eq result eof-indicator)
nil
(cons result (read-all-objects stream eof-indicator))))
然后,在 REPL 上调用函数后:
CL-USER> (read-my-file) ; Evaluation aborted on
#<SB-INT:SIMPLE-READER-PACKAGE-ERROR "Package ~A does not exist." {1003AB9273}>.
我收到此错误消息:
Package HISTORY-TREE does not exist.
Line: 3, Column: 45, File-Position: 110
Stream: #<SB-SYS:FD-STREAM for "file /home/pedro/miscellaneous/misc/symbolic-computation/history-to-read.lisp" {1003AB83A3}> [Condition of type SB-INT:SIMPLE-READER-PACKAGE-ERROR]
“历史树”只是写在文字上的东西。喜欢 foo.
此外,我尝试重现@RainerJoswig
1 - 因为两个文件都是 .lisp
并且读取它们的函数是相同的,为什么会这样?
2 - 为什么文本的 meaning/content 在这里很重要?
3 - 我该如何阅读这个文件?
Package HISTORY-TREE does not exist
该消息很明确:包 HISTORY-TREE
不存在。但是您尝试将符号读入这个不存在的包中,例如 HISTORY-TREE:OWNER
.
该文件中打印符号中提到的每个包都需要存在,才能成功读取该文件。
默认的 Common Lisp reader 不会仅通过读取符号来即时创建包。
示例:
CL-USER 130 > (read-from-string "bar::foo")
Error: Reader cannot find package BAR.
1 (continue) Create the BAR package.
2 Use another package instead of BAR.
3 Try finding package BAR again.
4 (abort) Return to top loop level 0.
Type :b for backtrace or :c <option number> to proceed.
Type :bug-form "<subject>" for a bug report template or :? for other options.
现在让我们使用错误重启来创建该包:
CL-USER 131 : 1 > :c 1
BAR::FOO
8
替代解决方案:
- 在读取数据之前创建包
- 不要打印带有包前缀的符号
- 使用自定义 reader,它可以创建包或将符号标记为具有未定义的包
备注
(defun read-all-objects (stream eof-indicator)
(let ((result (read stream nil eof-indicator)))
(if (eq result eof-indicator)
nil
(cons result (read-all-objects stream eof-indicator))))
例如可以这样写:
(defun read-all-objects (stream &aux (eof '#:eof))
(loop for item = (read stream nil eof)
until (eq item eof)
collect item))
好处:
- 这里不需要 EOF 参数,这里我们使用一个 uninterned 符号
- 列表项的数量不受堆栈深度的限制