0

我有这个代码:

(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 of making a function。但仍然显示相同的错误。

我希望任何人都可以帮助我。

4

1 回答 1

2

您可能将这三行代码写到 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. 如果您在编辑器中调用它,它会显示小提示“输入内容:”。

如您所见,不需要全局变量。如果您需要使用该给定值执行某些操作,请使用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"
NIL
于 2021-03-31T05:00:10.997 回答