0

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.

1
  • 1
    Common Lisp is a langauge, not an implementation. LispWorks is an implementation of Common Lisp. If you post an error, you need to give us a reproducible test case and how you ran your code. Commented Mar 31, 2021 at 4:01

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.