繁体   English   中英

在Common Lisp中循环遍历字符串中的行

[英]Looping over lines in a string in Common Lisp

我试图理解为什么这小段代码无法按预期工作。 我希望它能打印出“ foo”,但实际上我得到的是

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

NIL

我希望我使用的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))))))

您没有在循环体内放入任何东西。 您的函数读取一行( "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))))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM