Google 網路論壇不再支援新的 Usenet 貼文或訂閱項目,但過往內容仍可供查看。

Testing for EOF

瀏覽次數:6 次
跳到第一則未讀訊息

Robert L.

未讀,
2017年12月10日 凌晨4:54:502017/12/10
收件者:
> >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 則新訊息