Groups | Search | Server Info | Login | Register


Groups > comp.lang.scheme > #6553

Re: Newbie cluelessness continued...

From "B. Pym" <Nobody447095@here-nor-there.org>
Newsgroups comp.lang.lisp, comp.lang.scheme
Subject Re: Newbie cluelessness continued...
Date 2025-08-22 10:37 +0000
Organization A noiseless patient Spider
Message-ID <1089ha3$1gneb$1@dont-email.me> (permalink)
References <104fism$2ods3$1@dont-email.me> <104fl1g$2oqg5$1@dont-email.me>

Cross-posted to 2 groups.

Show all headers | View raw


B. Pym wrote:

> B. Pym wrote:
> 
> > Tim Bradshaw wrote:
> > 
> > >   (with-open-file (...)
> > >     (loop for line = (read-line stream nil stream)
> > >           until (eql line stream)
> > >           collect line))
> > 
> > Gauche Scheme
> > 
> > (use srfi-42)  ;; list-ec
> > 
> > (call-with-input-file "data.bak"
> >   (lambda (port)
> >     (list-ec (:port line port read-line) line)))
> 
> Sascha Wilde wrote:
> 
> > speaking of basic tools i would prefer using do rather when loop
> > (which IMHO is quite "unlispish") like:
> > 
> > (defun read-file (arg-file-name)
> >   (with-open-file (stream arg-file-name :direction :input)
> >     (do ((line nil (read-line stream nil stream))
> >          (r nil (push line r)))
> 
> Should have used cons instead of push.
> 
> >           ((eql line stream) (cdr (nreverse r))))))
> 
> The cdr removes the bogus first element (nil).
> 
> Scheme
> 
> (define (file->lines file-name)
>   (with-input-from-file file-name
>     (lambda()
>       (do ((line #f (read-line))
>            (r '() (cons line r)))
>         ((eof-object? line) (cdr (reverse r)))))))

(define (file->lines file-name)
  (with-input-from-file file-name (lambda()
    (Do ((line (read-line) <>)
         (r '() (cons line r)))
      ((eof-object? line) @ r)))))

Given:

(define-syntax Do-aux
  (syntax-rules (<> @ values)
    [(_ ((a b <>) d ...) (seen ...) z ...)
     (Do-aux (d ...) (seen ... (a b b)) z ...) ]
    [(_ ((a b c ...) d ...) (seen ...) z ...)
     (Do-aux (d ...) (seen ... (a b c ...)) z ...) ]
    [(_ ((a) d ...) (seen ...) z ...)
     (Do-aux (d ...) (seen ... (a '())) z ...) ]
    [(_ (a d ...) (seen ...) z ...)
     (Do-aux (d ...) (seen ... (a '())) z ...) ]
    [(_ () seen (a b ... @ (values x ...)) z ...)
     (Do-aux () seen (a b ... (values (reverse~ x) ...)) z ...) ]
    [(_ () seen (a b ... @ xs) z ...)
     (Do-aux () seen (a b ... (reverse xs)) z ...) ]
    [(_ () seen till body ...)
     (do seen till body ...) ]))
(define-syntax Do
  (syntax-rules ()
    [(_ specs till body ...)
     (Do-aux specs () till body ...) ]))

-- 
[T]he problem is that lispniks are as cultish as any other devout group and
basically fall down frothing at the mouth if they see [heterodoxy].
  --- Kenny Tilton
The good news is, it's not Lisp that sucks, but Common Lisp. --- Paul Graham

Back to comp.lang.scheme | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Re: Newbie cluelessness continued... "B. Pym" <Nobody447095@here-nor-there.org> - 2025-07-07 04:36 +0000
  Re: Newbie cluelessness continued... "B. Pym" <Nobody447095@here-nor-there.org> - 2025-07-07 05:13 +0000
    Re: Newbie cluelessness continued... "B. Pym" <Nobody447095@here-nor-there.org> - 2025-08-22 10:37 +0000
  Re: Newbie cluelessness continued... "B. Pym" <Nobody447095@here-nor-there.org> - 2025-07-07 18:44 +0000

csiph-web