【问题标题】:How to properly ask an input from user in LispWorks?如何在 LispWorks 中正确询问用户的输入?
【发布时间】:2021-03-31 01:53:43
【问题描述】:

我有这个代码:

(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 的函数。但仍然显示相同的错误。

希望有人能帮助我。

【问题讨论】:

  • Common Lisp 是一种语言,而不是一种实现。 LispWorks 是 Common Lisp 的一个实现。如果您发布错误,您需要给我们一个可重现的测试用例以及您是如何运行代码的。

标签: input lisp common-lisp lispworks


【解决方案1】:

你可能把这三行写进了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

【讨论】:

    猜你喜欢
    • 2015-10-18
    • 2018-08-09
    • 2014-07-09
    • 1970-01-01
    • 1970-01-01
    • 2013-06-20
    • 1970-01-01
    • 2015-06-28
    • 1970-01-01
    相关资源
    最近更新 更多