Page 1 of 1

Reading standard input

Posted: Sat Feb 05, 2011 3:13 am
by michael
Hi folks, new to clisp (just started today in fact).

Question...

How does one read standard input ie: my_script < file.in

So far I have:

Code: Select all

#!/usr/bin/env clisp
# chmod +x my_script

(loop for line = (read-line stream nil :eof)
      until (eq line :eof)
      print line ))
(bye)

Re: Reading standard input

Posted: Sat Feb 05, 2011 5:14 am
by ramarren
michael wrote:Hi folks, new to clisp (just started today in fact).
The language is called Common Lisp, which is usually shortened to CL. CLISP is a name of a particular implementation of the ANSI CL standard.
michael wrote:How does one read standard input ie: my_script < file.in
By reading from a stream bound to global variable *terminal-io*. But do note that this is not really how CL is commonly used. Particularly when starting you should primarily use the REPL. Read a book like Practical Common Lisp for an introduction.

Re: Reading standard input

Posted: Sat Feb 05, 2011 7:32 am
by vanekl
asked and answered on c.l.l.

Re: Reading standard input

Posted: Sat Feb 05, 2011 9:54 am
by jstoddard
I have to second that the REPL is invaluable; you're missing out on one of the most useful parts of Lisp programming. That said, if you were a conformist, you probably wouldn't be learning Lisp anyway. :)

I'm new as well; just a few days into Lisp, and just finished the "Practical Common Lisp" book, but here goes a shot:

Code: Select all

#!/usr/bin/clisp
(loop for line = (read-line *terminal-io* nil :eof)
     until (eq line :eof)
     do (print line))
read-line only needs the name of the stream (not the word "stream") to be happy. You need that nil before :eof to tell it not to give an error when it reaches the end of the file.

You were also missing a parenthesis on the left side of print line, and I believe the keyword "do" is appropriate there. This works for me; when I direct a file into it, it prints out that file's contents...

Re: Reading standard input

Posted: Sat Feb 05, 2011 5:30 pm
by michael
vanekl wrote:asked and answered on c.l.l.
You bet - the more data I have, the more I learn. Is there a problem with that?

Re: Reading standard input

Posted: Sat Feb 05, 2011 5:32 pm
by michael
jstoddard wrote:I have to second that the REPL is invaluable; you're missing out on one of the most useful parts of Lisp programming. That said, if you were a conformist, you probably wouldn't be learning Lisp anyway. :)

I'm new as well; just a few days into Lisp, and just finished the "Practical Common Lisp" book, but here goes a shot:

Code: Select all

#!/usr/bin/clisp
(loop for line = (read-line *terminal-io* nil :eof)
     until (eq line :eof)
     do (print line))
read-line only needs the name of the stream (not the word "stream") to be happy. You need that nil before :eof to tell it not to give an error when it reaches the end of the file.

You were also missing a parenthesis on the left side of print line, and I believe the keyword "do" is appropriate there. This works for me; when I direct a file into it, it prints out that file's contents...
Thanks, I've just began reading Practical Lisp too. So, here we go!