开发者

How can i use "loop for" in this situation?

The following code will raise: SYSTEM::%E开发者_运维问答XPAND-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!


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.


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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜