簡體   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