我如何使用“循环”在这种情况下?

发布于 2024-12-06 04:58:41 字数 563 浏览 2 评论 0原文

以下代码将引发: 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 技术交流群。

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

发布评论

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

评论(2

赏烟花じ飞满天 2024-12-13 04:58:41

您需要像这样删除最外面的一组括号。

(loop for line = (read-line stream nil)
         until (null line)
         do 
            (setq num (substring line 6))
            (print line))

循环体周围有一个隐式的 progn ,当您添加额外的括号时,读者将期望列表中的第一个内容(在本例中 (setq num (substring line 6))< /code> 是一个带有函数的符号。显然 setq 形式不符合该标准,因此它会失败,尽管我不确定为什么它告诉你它应该是一个 lambda 表达式。

风格注释:

不要像你一样使用括号Java 或 C++ 中的花括号

do (
     &body
   )

是不好的 Lisp 风格,请像我在答案中那样关闭最后一行的所有括号,并在左括号开始的同一行上打开括号。

You need to remove the outermost set of parens like this.

(loop for line = (read-line stream nil)
         until (null line)
         do 
            (setq num (substring line 6))
            (print line))

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 the setq 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++!

do (
     &body
   )

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.

猥︴琐丶欲为 2024-12-13 04:58:41

您需要 PROGN 特殊形式 来按顺序计算所有这些表达式。

...
do (progn
     (setq num (substring line 6))
     (print line))
...

You need the PROGN special form to evaluate all of those expressions in order.

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