在Common Lisp中循环使用字符串中的行

Fah*_*tha 1 lisp common-lisp

我试图理解为什么这一小段代码不能按预期工作.我希望它打印出"foo",但实际上我得到的是

CL-USER> (stringloop)
null output T
 line output NIL

NIL
Run Code Online (Sandbox Code Playgroud)

我希望我do错了,但我无法弄清楚是什么.

(defun stringloop ()
(with-input-from-string (s "foo" :index j )
  (do ((line (read-line s nil) ;; var init-form
         (read-line s nil))) ;; step=form
      ((null line) (progn (format t "null output ~a~% "(null line)) (format t "line output ~a~% " line))))))
Run Code Online (Sandbox Code Playgroud)

ang*_*gus 6

你没有在循环体中放任何东西.你的函数读取一行("foo"),不执行任何操作,然后读取另一行(nil),终止条件变为true,并打印空行.

运行此修改版本以查看发生的情况:

     (defun stringloop ()
       (with-input-from-string (s "foo")
         (do ((line (read-line s nil) ;; var init-form
                    (read-line s nil))) ;; step=form
             ((null line) (format t "termination condition - line: ~s~% " line))
           (format t "in loop - line: ~s~%" line))))
Run Code Online (Sandbox Code Playgroud)