Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 442
chope_chope Avatar asked Oct 03 '14 01:10

chope_chope


People also ask

What is Terpri in Lisp?

Print a carriage return. This function prints a new line to the command prompt area, and returns nil. Example. Code.

What is SETQ in Lisp?

1.9. 2 Using setq This special form is just like set except that the first argument is quoted automatically, so you don't need to type the quote mark yourself. Also, as an added convenience, setq permits you to set several different variables to different values, all in one expression.

How do you say hello world in Lisp?

Unfortunately, Lisp has many flavors which means the following implementation of Hello World will likely only be applicable to handful of those flavors: (format t "Hello, World!") (format t "Hello, World!")

How do I print a new line in Lisp?

Use the TERPRI function (the name stands for "terminate printing", as it's intended to be used to terminate a line of output). You could also use FRESH-LINE . This prints a newline unless you're already at the start of a line.


1 Answers

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.

like image 55
Mark Karpov Avatar answered Sep 28 '22 05:09

Mark Karpov