在Common Lisp中以字符串形式在行上循环
我试图理解为什么这一小段代码不能按预期工作。 我希望它能打印出“foo”,但事实上我得到的是在Common Lisp中以字符串形式在行上循环,lisp,common-lisp,Lisp,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
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
),终止条件变为真,然后打印空行
运行此修改版本以查看发生了什么:
(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))))
你没有把任何东西放在环身上。函数读取一行(
“foo”
),对其不做任何操作,然后读取另一行(nil
),终止条件变为真,然后打印空行
运行此修改版本以查看发生了什么:
(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))))
嗨,安格斯。非常感谢你的解释。我会问你是否还有其他问题。嗨,安格斯。我明白我的错误了。从PCL“当结束测试表单的计算结果为true时,将计算结果表单,并将最后一个结果表单的值作为DO表达式的值返回。”我不知何故得到了这样的印象,即结果表单在每一步都进行了计算,但实际上仅在最后一步,即行为null时才进行计算。所以我的打印条件应该在循环体中,对吗?嗨,安格斯。非常感谢你的解释。我会问你是否还有其他问题。嗨,安格斯。我明白我的错误了。从PCL“当结束测试表单的计算结果为true时,将计算结果表单,并将最后一个结果表单的值作为DO表达式的值返回。”我不知何故得到了这样的印象,即结果表单在每一步都进行了计算,但实际上仅在最后一步,即行为null时才进行计算。所以我的打印条件应该在循环体中,对吗?