我如何使用“循环”在这种情况下?
以下代码将引发: SYSTEM::%EXPAND-FORM: (SETQ NUM (SUBSTRING LINE 6)) 应该是一个 lambda 表达式。
(defun good-red ()
(let ((tab (make-hash-table)))
(dotimes (i 50) (setf (gethash (+ i 1) tab) 0))
(with-open-file (stream "ssqHitNum.txt")
(loop for line = (read-line stream nil)
until (null line)
do (
(setq num (substring line 6))
(print line)
)))))
如果我如下更改“do”,它就会起作用。然而,我需要在这里做很多事情。
...
do (print line)
...
真挚地!
The following code will raise: SYSTEM::%EXPAND-FORM: (SETQ NUM (SUBSTRING LINE 6)) should be a lambda expression.
(defun good-red ()
(let ((tab (make-hash-table)))
(dotimes (i 50) (setf (gethash (+ i 1) tab) 0))
(with-open-file (stream "ssqHitNum.txt")
(loop for line = (read-line stream nil)
until (null line)
do (
(setq num (substring line 6))
(print line)
)))))
If i change the "do" as below, it works. However, i need to do many things here.
...
do (print line)
...
Sincerely!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要像这样删除最外面的一组括号。
循环体周围有一个隐式的
progn
,当您添加额外的括号时,读者将期望列表中的第一个内容(在本例中(setq num (substring line 6))< /code> 是一个带有函数的符号。显然
setq
形式不符合该标准,因此它会失败,尽管我不确定为什么它告诉你它应该是一个 lambda 表达式。风格注释:
不要像你一样使用括号Java 或 C++ 中的花括号
是不好的 Lisp 风格,请像我在答案中那样关闭最后一行的所有括号,并在左括号开始的同一行上打开括号。
You need to remove the outermost set of parens like this.
The loop body has an implicit
progn
around it, when you add the extra parens the reader will expect the first thing in the list (in this case(setq num (substring line 6))
to be a symbol with a function. Obviously thesetq
form does not meet that criteria and so it will fail, though I'm not sure why it tells you it should be a lambda expression.A style note:
Do not use parens as you would curly braces in Java or C++!
is bad Lisp style, close all parens on the last line as I have in my answer and open parens on the same line as the form the left paren begins.
您需要 PROGN 特殊形式 来按顺序计算所有这些表达式。
You need the PROGN special form to evaluate all of those expressions in order.