Deutsch   English   Français   Italiano  
<vbg0g7$vtpn$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: "B. Pym" <Nobody447095@here-nor-there.org>
Newsgroups: comp.lang.lisp,comp.lang.scheme
Subject: Re: Unable to read a list with 'with-open-file'
Date: Fri, 6 Sep 2024 22:42:23 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 46
Message-ID: <vbg0g7$vtpn$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Sat, 07 Sep 2024 00:42:23 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="c1dfddce755618b68a420d365e7caff7";
	logging-data="1046327"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/KOv84CY7zd0BI6+IDoyVe"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:WaLrWm+nlucV8etxmeRkb3O3Nc4=
Bytes: 2033

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