Search code examples
lispcommon-lisplispworks

How to read user input in Lisp


I'm very new to Lisp and am trying to write a program that simply asks a user to enter 3 numbers and then sums them and prints the output.

I've read that you can you a function like:

(defvar a)

(setq a (read))

To set a variable in Lisp, but when I try to compile my code using LispWorks I get the following error:

End of file while reading stream #<Concatenated Stream, Streams = ()>

I feel like this should be relatively simple and have no idea where I'm going wrong.


Solution

  • I've not worked with LispWorks, so it's only a guess.

    When compiler traverses your code it gets to the line (setq a (read)), it tries to read input, but there is no input stream while compiling, thus you get an error.

    Write a function:

    (defvar a)
    
    (defun my-function ()
      (setq a (read))
    

    It should work.