7

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.

1
  • 3
    It's hard for us to help you if we can't see your code. Commented Oct 3, 2014 at 1:11

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

why should a compiler execute code, instead of compiling it?
6

This should evaluate properly in your Lisp:

(defun read-3-numbers-&-format-sum ()
  (flet ((prompt (string)
           (format t "~&~a: " string)
           (finish-output)
           (read nil 'eof nil)))
    (let ((x (prompt "first number"))
          (y (prompt "second number"))
          (z (prompt "third number")))
      (format t "~&the sum of ~a, ~a, & ~a is:~%~%~a~%"
              x y z (+ x y z)))))

Simply evaluate the above function definition, then run the form:

(read-3-numbers-&-format-sum)

at your LispWorks interpreter.

9 Comments

You'll notice that I don't set any variables on the global scope with a special form such as SETQ or DEFVAR. To do so, in short, would be bad programming. I'll be happy to write a walkthrough of this code, should it become of interest to anyone.
Changing values of special variables is not 'bad programming' per se. State is necessary in many applications and in OP's case it is not bad, since his/her program is just a test.
A grateful nod to @uselpa for editing my original reply as to properly handle the indentation; I see from the edit how it is to be done and will make use of this knowledge. I thank you sincerely.
You can find information about basic terminology here. I'm sorry if I offended you in some way, I didn't mean it. defvar and defparameter are in language not as historical garbage, global (a.k.a special) variables are part of the language, and people do modify them quite often. Yes, Common Lisp is a functional language, but you must admit that Common Lisp is not pure language. In fact, it is far more imperative then Clojure for example, not to mention Haskell. Thus, it is OK to use assignment in some cases, especially for such toy programs.
Please note that I didn't say that your code is bad or you're wrong. I only said that 'Changing values of special variables is not 'bad programming' per se.' for reasons that I've described above.
|

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.