Re: Unable to read a list with 'with-open-file'

Liste des GroupesRevenir à cl lisp 
Sujet : Re: Unable to read a list with 'with-open-file'
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lisp comp.lang.scheme
Date : 26. Jun 2025, 02:33:17
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <103i80s$33krc$1@dont-email.me>
References : 1
User-Agent : XanaNews/1.18.1.6
B. Pym wrote:

Tim Bradshaw wrote:
 
If you want to write a function that returns the lines in the file do
this:
 
(defun read-lines-from-file (file)
  ;; Return a list of all the lines in FILE.  FILE is either a string
or
  ;; a pathname
  (with-open-file (in file :direction ':input)
    ;; no real line can be NIL, so we don't need to worry about
    ;; inventing a unique return value here
    (loop for line = (read-line in nil nil)
          while line collect line)))
...
Note for CLL people: I think this is a great use of LOOP.  It's *so*
easy to see what is happening here:
 
   loop for line = <get next line from file, NIL on EOF>
        while line collect line
 
Of course it's not pure functional Lisp.  But *so what*?
 
Gauche Scheme
 
(use gauche.generator)
 
(with-input-from-file "output.dat" (lambda()
  (generator->list read-line)))
 
 
Another way:
 
(use file.util)
 
(file->list read-line "output.dat")
 
 
Another way:
 
(define (collect-file-lines file)
  (with-input-from-file file (lambda()
    (let go ()
      (if (eof-object? (peek-char))
        '()
        (cons (read-line) (go)))))))

(use srfi-42) ;; list-ec

(call-with-input-file "data.bak"
  (lambda (in-port)
    (list-ec (:port line in-port read-line) line)))
 


Date Sujet#  Auteur
6 Sep 24 * Re: Unable to read a list with 'with-open-file'2B. Pym
26 Jun 25 `- Re: Unable to read a list with 'with-open-file'1B. Pym

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal