簡體   English   中英

如何在 LispWorks 中正確詢問用戶的輸入?

[英]How to properly ask an input from user in LispWorks?

我有這個代碼:

(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。 但仍然顯示相同的錯誤。

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

您可能將這三行代碼寫到 Editor 中,然后編譯它。

因此,您可以將此 function 寫入編輯器:

(defun get-input ()
  (format t "This is your input: ~a" (read)))

編譯編輯器並從 Listener (REPL) 調用此 function。

CL-USER 6 > (get-input)
5
This is your input: 5
NIL

您也可以像這樣使用*query-io* stream:

(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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM