首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我不能在通用Lisp中读取这个文件?

为什么我不能在通用Lisp中读取这个文件?
EN

Stack Overflow用户
提问于 2021-06-05 18:44:28
回答 1查看 189关注 0票数 0

我正在尝试使用Common with-open-file读取文件。在我的环境中,我使用的是SBCL、Emacs和Slime。

我可以使用以下方法打开像这一个 (书签到read.lisp)这样的文件:

代码语言:javascript
复制
(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上的函数之后,我得到了预期的内容:

代码语言:javascript
复制
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。但是,如果我对这个其他档案 (history-to-read.lisp)尝试相同的操作,就会得到一个错误。

我只需要改变位置:

代码语言:javascript
复制
(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上的函数之后:

代码语言:javascript
复制
CL-USER> (read-my-file) ; Evaluation aborted on
#<SB-INT:SIMPLE-READER-PACKAGE-ERROR "Package ~A does not exist." {1003AB9273}>.

我收到以下错误消息:

包历史树不存在。 行: 3,列: 45,档案位置: 110 文件/home/pedro/miscellaneous/misc/symbolic-computation/history-to-read.lisp“{1003AB83A3}> SB-INT类型的条件:简单读取器-包错误

“历史树”只是写在课文上的东西。就像福。

此外,我尝试复制@RainerJoswig 这里所描述的建议。不幸的是,同样的问题也发生了。

因为这两个文件都是.lisp,读取它们的函数是相同的,为什么会发生这种情况呢?

2-为什么案文的含义/内容在这里很重要?

3-我怎么看这个文件?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-06 08:45:45

包历史树不存在。

这个信息很清楚:包HISTORY-TREE不存在。但是您尝试将符号读入这个不存在的包中,例如HISTORY-TREE:OWNER

在该文件中的打印符号中提到的每个包都需要存在,才能成功地读取该文件。

默认的Common读取器不只是通过读取符号来动态创建包。

示例:

代码语言:javascript
复制
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.

现在,让我们使用错误重新启动来创建这个包:

代码语言:javascript
复制
CL-USER 131 : 1 > :c 1
BAR::FOO
8

备选解决办法:

  1. 在读取数据之前创建包
  2. 不要打印带有包前缀的符号
  3. 使用自定义读取器,它可以将包或符号标记为具有未定义的包。

备注

代码语言:javascript
复制
(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))))

例如,我们可以写这样的东西:

代码语言:javascript
复制
(defun read-all-objects (stream &aux (eof '#:eof))
  (loop for item = (read stream nil eof)
        until (eq item eof)
        collect item))

好处:

  • 这里不需要EOF参数,这里我们使用了一个非uninterned符号
  • 列表项的数量不受堆栈深度的限制。
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67852877

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档