我有这样的代码:
(defvar x)
(setq x (read))
(format t "this is your input: ~a" x)它在Common Lisp中是可以工作的,但是在LispWorks中它显示这个错误:
End of file while reading stream #<Synonym stream to
*BACKGROUND-INPUT*>.我的意思是,我尝试过这个:创建函数的How to read user input in Lisp。但仍然显示相同的错误。
我希望任何人都能帮助我。
发布于 2021-03-31 13:00:11
您可能将这三行代码写入到Editor中,然后对其进行编译。
因此,您可以将此函数写入Editor:
(defun get-input ()
(format t "This is your input: ~a" (read)))编译编辑器并从侦听器调用此函数(REPL)。
CL-USER 6 > (get-input)
5
This is your input: 5
NIL您也可以像这样使用*query-io*流:
(format t "This is your input: ~a" (read *query-io*))如果您在Listener中调用此行,它的行为类似于read。如果您在Editor中调用它,它会显示一个小提示"Enter something:“。
如您所见,不需要全局变量。如果需要对给定值执行某些操作,请使用let,它会创建本地绑定:
(defun input-sum ()
(let ((x (read *query-io*))
(y (read *query-io*)))
(format t "This is x: ~a~%" x)
(format t "This is y: ~a~%" y)
(+ x y)))还可以考虑使用read-line,它接受输入并将其作为字符串返回:
CL-USER 19 > (read-line)
some text
"some text"
NIL
CL-USER 20 > (read-line)
5
"5"
NILhttps://stackoverflow.com/questions/66880602
复制相似问题