LISP/Nyquist 打开文件并逐行读取
我正在努力打开一个文件,并读取每一行直到 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 行和第 2 行 - 分配在循环中使用的变量、它们的初始值以及如何递增它们。在本例中为
fp
和ex
。fp
不递增,并且ex
被初始化并递增一个从 fp 读取的(我不知道尾随的
nil 是什么)
是 for) - 第 3 行 - 循环的退出条件和
返回表达式
列表,所有这些都在退出时进行计算,并返回最后一个。在这种情况下,当ex == null
时将发生退出,然后返回nil
- 第 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:
- lines 1 and 2 - variables assigned for use in the loop, their initial values and how to increment them. In this case
fp
andex
.fp
is not incremented, andex
is initialized and incremented by oneread
from from fp (I have no idea what that trailingnil
is for) - 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 whenex == null
and thennil
will be returned - 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
奈奎斯特不是基于 Common Lisp。它基于早期的 XLISP。它只支持简单的LOOP,不支持Common Lisp的复杂LOOP。
请参阅奈奎斯特手册:
http://www.cs.cmu .edu/~rbd/doc/nyquist/part19.html#226
它的LOOP语法是:
它不支持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:
It does not support the extended LOOP syntax of Common Lisp.