read-line in common lisp
Asked Answered
B

5

5

I want to read input from STDIN, and just read what it is: if input is a list, then what is read is a list. However, the read-line function seems always return a string! For example: in clisp interactive envrironment:

(read-line)

I input:

("(define M ::int )" "(define X ::int )")

The it will give me back a string:

"(\"(define M ::int )\" \"(define X ::int )\")" ;

What I want is still the original list: ("(define M ::int )" "(define X ::int )")

So How to make the read-line read in what the input it was?

Bailes answered 19/4, 2011 at 19:49 Comment(0)
I
2
(let ((a read)))
   (eval a))

(+ 2 2 2)

=> 6

there's a reason they call it a READ EVAL PRINT LOOP.

Isometric answered 22/4, 2011 at 4:59 Comment(0)
O
14

Try simply with:

(read)

That should work

Ozzy answered 19/4, 2011 at 19:55 Comment(0)
I
2
(let ((a read)))
   (eval a))

(+ 2 2 2)

=> 6

there's a reason they call it a READ EVAL PRINT LOOP.

Isometric answered 22/4, 2011 at 4:59 Comment(0)
A
2

(read-line) returns a string terminated by a new-line. (read) is the Lisp parser.

Assist answered 20/7, 2014 at 10:14 Comment(0)
M
2

Letting the user enter a Lisp expression is certainly risky. Therefore, I would protect the read function by wrapping it inside an ignore-errors:

(ignore-errors (read))

This way, if the user enters e.g. ")" (without the quotes), the interpreter will not enter the debug loop, but simply print an error message and return nil.

Monarski answered 8/1, 2020 at 18:49 Comment(0)
J
1

As others have pointed out, (read) does what you need. Here's why: READ takes an optional argument specifying the input stream from which to read. It defaults to *STANDARD-INPUT*, designating STDIN, which is why it'll work without arguments, but you could specify other streams to read from (such as a file). E.g., (with-open-file (s path) (read s)).

Jabalpur answered 8/1, 2020 at 15:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.