Path: news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: "B. Pym" Newsgroups: comp.lang.lisp,comp.lang.scheme Subject: Re: Unable to read a list with 'with-open-file' Date: Thu, 26 Jun 2025 01:33:17 -0000 (UTC) Organization: A noiseless patient Spider Lines: 56 Message-ID: <103i80s$33krc$1@dont-email.me> References: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Injection-Date: Thu, 26 Jun 2025 03:33:18 +0200 (CEST) Injection-Info: dont-email.me; posting-host="e164c7e8c4615b560d0a58ee26f330d2"; logging-data="3265388"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19gyYkqbCpa8/EKvpfR9l5c" User-Agent: XanaNews/1.18.1.6 Cancel-Lock: sha1:7vnG6P1PWU0M3l2UfpyR7bRJNrY= 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 = > > 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)))