Reading standard input

Discussion of Common Lisp
Post Reply
michael
Posts: 3
Joined: Sat Feb 05, 2011 3:06 am

Reading standard input

Post by michael » Sat Feb 05, 2011 3:13 am

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)

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: Reading standard input

Post by ramarren » Sat Feb 05, 2011 5:14 am

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.

vanekl
Posts: 12
Joined: Wed Dec 15, 2010 10:25 am

Re: Reading standard input

Post by vanekl » Sat Feb 05, 2011 7:32 am

asked and answered on c.l.l.

jstoddard
Posts: 20
Joined: Fri Jan 28, 2011 6:13 pm

Re: Reading standard input

Post by jstoddard » Sat Feb 05, 2011 9:54 am

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...

michael
Posts: 3
Joined: Sat Feb 05, 2011 3:06 am

Re: Reading standard input

Post by michael » Sat Feb 05, 2011 5:30 pm

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?
Last edited by michael on Sat Feb 05, 2011 5:33 pm, edited 1 time in total.

michael
Posts: 3
Joined: Sat Feb 05, 2011 3:06 am

Re: Reading standard input

Post by michael » Sat Feb 05, 2011 5:32 pm

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!

Post Reply