Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Testing for EOF

6 views
Skip to first unread message

Robert L.

unread,
Dec 10, 2017, 4:54:50 AM12/10/17
to
> >I am starting to write Lisp and am trying to read the lines of a file
> >into a list of strings such that each element of the list is one line
> >from the file. I have figured out how to read lines with read-line,
> >but I can't fugure out how to test for the end of the file. I'm sure
> >it must be easy but I've looked through Steele's book on Common Lisp
> >and through the FAQ and I can't find it. I'd appreciate it if someone
> >could give me a tip.
>
> READ-LINE will return NIL (or whatever value you specify in the <eof-value>
> parameter) when it reaches EOF.
>
> (defun list-lines (filename)
> (with-open-file (stream filename :direction :input)
> (loop for line = (read-line stream)
> while (not (null line))
> collect line)))

(require srfi/42) ; list-ec

(define (lines-of-file filename)
(call-with-input-file filename
(lambda (in)
(list-ec (:port line in read-line) line))))

--
[Sweden] now has the second-highest "official" rape rate of any country. Its
53.2 rapes per 100,000 inhabitants is five times the United States' rate and is
only surpassed by Lesotho, a tiny nation in the middle of southern Africa.
http://archive.org/details/nolies
0 new messages