I have this code:
(defvar x)
(setq x (read))
(format t "this is your input: ~a" x)
It kinda work in Common Lisp but LispWorks it is showing this error:
End of file while reading stream #<Synonym stream to
*BACKGROUND-INPUT*>.
I mean I tried this: How to read user input in Lisp of making a function. But still shows the same error.
I hope anyone can help me.
You probably wrote these three lines into Editor and then compiled it.
So, you can write this function into Editor:
(defun get-input ()
(format t "This is your input: ~a" (read)))
Compile Editor and call this function from Listener (REPL).
CL-USER 6 > (get-input)
5
This is your input: 5
NIL
You can also use *query-io*
stream like this:
(format t "This is your input: ~a" (read *query-io*))
If you call this line in Listener, it behaves like read
. If you call it in Editor, it shows small prompt "Enter something:".
As you can see, no global variable was needed. If you need to do something with that given value, use let, which creates local bindings:
(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)))
Consider also using read-line, which takes input and returns it as string:
CL-USER 19 > (read-line)
some text
"some text"
NIL
CL-USER 20 > (read-line)
5
"5"
NIL