LISP/Nyquist 打开文件并逐行读取

发布于 2024-12-28 12:35:20 字数 1401 浏览 1 评论 0原文

我正在努力打开一个文件,并读取每一行直到 EOF。我使用的是 nyquist,它基于 XLISP,缺少 Common Lisp 的 with-open-file。我的代码是:

(let ((in_file (open "/home/soodesune/testfile.csv" :if-does-not-exist nil)))
    (loop for line = (read-line in_file)
         while line do (print line))
    (close in_file))

我从奈奎斯特得到的输出是:

error: unbound variable - LINE

我希望得到一些帮助来理解我哪里出错了


注意: 我也尝试过:

(loop for line = (read-line in_file nil)

但这给出了:

error: too many arguments

更新:

使用 Rainer Joswig 提供的信息和链接,我能够找到可能用来打开文件并逐行读取它的典型代码:

  1 (do* ((fp (open "test.dat" :direction :input))
  2       (ex (read fp nil) (read fp nil)))
  3  ((null ex) (close fp) nil)
  4  (print ex))

do 函数具有三个基本部分:

  1. 第 1 行和第 2 行 - 分配在循环中使用的变量、它们的初始值以及如何递增它们。在本例中为 fpexfp 不递增,并且 ex 被初始化并递增一个从 fp 读取的 (我不知道尾随的 nil 是什么) 是 for)
  2. 第 3 行 - 循环的退出条件和返回表达式 列表,所有这些都在退出时进行计算,并返回最后一个。在这种情况下,当 ex == null 时将发生退出,然后返回 nil
  3. 第 4 行 - 循环的实际主体。在这种情况下不言自明。

注意:do*do 的不同之处在于,它允许在一个表达式中分配的变量在后面的表达式中使用,就像 fp 的使用方式一样在 ex 的作业中,

我对任何类型的 LISP 都是一窍不通,所以如果以上有任何错误,请指出。

I'm struggling to open a file, and read each line until EOF. I'm using nyquist, which is based on XLISP, and lacks Common Lisp's with-open-file. My code is:

(let ((in_file (open "/home/soodesune/testfile.csv" :if-does-not-exist nil)))
    (loop for line = (read-line in_file)
         while line do (print line))
    (close in_file))

the output im getting from nyquist is:

error: unbound variable - LINE

I'd love some help understanding where I'm going wrong


Note:
I also tried:

(loop for line = (read-line in_file nil)

But that gives:

error: too many arguments

UPDATE:

Using the information and link that Rainer Joswig provided I was able to find typical code one might use to open a file and read it line by line:

  1 (do* ((fp (open "test.dat" :direction :input))
  2       (ex (read fp nil) (read fp nil)))
  3  ((null ex) (close fp) nil)
  4  (print ex))

The do function has three basic parts:

  1. lines 1 and 2 - variables assigned for use in the loop, their initial values and how to increment them. In this case fp and ex. fp is not incremented, and ex is initialized and incremented by one read from from fp (I have no idea what that trailing nil is for)
  2. line 3 - The exit condition for the loop, and a list of return expressions all of which are evaluated on exit, and the last of which is returned. In this case exit will occur when ex == null and then nil will be returned
  3. line 4 - The actual body of the loop. Self explanatory in this case.

note: do* differs from do in that it allows variables assigned in one expression to be used in a later one, like how fp is used in the assignment of ex

I'm a total n00b at any kind of LISP, so if any of the above is wrong, please speak up.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老街孤人 2025-01-04 12:35:20

奈奎斯特不是基于 Common Lisp。它基于早期的 XLISP。它只支持简单的LOOP,不支持Common Lisp的复杂LOOP。

请参阅奈奎斯特手册:

http://www.cs.cmu .edu/~rbd/doc/nyquist/part19.html#226

它的LOOP语法是:

(loop expr...)

它不支持Common Lisp的扩展LOOP语法。

Nyquist is not based on Common Lisp. It is based on an early XLISP. It only supports a simple LOOP and not the complex LOOP of Common Lisp.

See the Nyquist manual:

http://www.cs.cmu.edu/~rbd/doc/nyquist/part19.html#226

Its LOOP syntax is:

(loop expr...)

It does not support the extended LOOP syntax of Common Lisp.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文