input - 将输入字符串分配并打印到变量。口齿不清

标签 input lisp common-lisp sbcl buffered

我希望我的程序请求一个表达式,将输入的字符串分配给变量“exp”,然后打印该表达式。

但是我遇到了一些麻烦。我首先尝试使用(阅读)

(princ "Enter a expression to be evaluated.")
(setf exp (read))
(princ exp)

但是,当我使用这段代码时,会发生这种情况。

Hello this is an expression            ;This is what I input
Enter a expression to be evaluated.HELLO
T

然后我尝试使用 (read-line),但是当我这样做时,似乎根本没有要求我输入。

(princ "Enter a expression to be evaluated.")
(setf exp (read-line))
(princ exp)

得到

Enter a expression to be evaluated.
T

程序刚刚结束。

经过一些回答我想出了这个

(defun get-input (prompt)
  (clear-input)                     
  (write-string prompt)             
  (finish-output)                   
  (setf exp (read-line)))           

(get-input "Enter an expression: ")
(princ exp)

但是当我运行它时会发生以下情况

My first sentence                            ;My first input
Enter an expression: My second sentence      ;it then asks for input, i do so
My second sentence                           ;my second input is printed back at me
T

最佳答案

这是一种常见问题解答。

输出可以被缓冲。使用 FINISH-OUTPUT 确保输出确实到达了目的地。

READ 读取 Lisp s 表达式。它返回相应的数据结构。它仅在您输入有效的 s 表达式时有用。

READ-LINE 读取一行并返回一个字符串。

例子:

* 
(defun ask (&optional (message "Input: "))
  (clear-input)           ; get rid of pending input
  (write-string message)  ;
  (finish-output)         ; make sure output gets visible
  (read-line))            ; read a line as a string

ASK
* (ask "Name: ")
Name: Rainer

"Rainer"
NIL

文件p.lisp:

(defun get-input (prompt)
  (clear-input)
  (write-string prompt)
  (finish-output)
  (read-line))

(write-string (get-input "Enter a sentence: "))
(finish-output)

输出

* (load "/tmp/p.lisp")
Enter a sentence: foo is not a bar
foo is not a bar
T

关于input - 将输入字符串分配并打印到变量。口齿不清,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27203874/

相关文章:

javascript - JQuery/JavaScript - 从另一个按钮点击事件触发按钮点击

python - 代码在开始时无需输入即可运行,为什么?

lisp - 你能告诉我如何用 lisp 重写函数吗?

LISP 帮助!糖果机分配器

html - 如何更改输入框的大小?

html - 输入多个的 CSS 样式

c# - 管理代码 Assets

lisp - apply & funcall - 不同的结果

lisp - 普通口齿不清 : function A passes a function to B which passes it to C which invokes the function

common-lisp - 惊喜打印到字符串(普通的 lisp)