• Re: Newbie cluelessness continued...

    From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp,comp.lang.scheme on Mon Jul 7 04:36:38 2025
    From Newsgroup: comp.lang.scheme

    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)))
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp,comp.lang.scheme on Mon Jul 7 05:13:21 2025
    From Newsgroup: comp.lang.scheme

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

    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp,comp.lang.scheme on Mon Jul 7 18:44:37 2025
    From Newsgroup: comp.lang.scheme

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

    Another way:

    (define (file->lines file-name)
    (with-input-from-file file-name
    (lambda()
    (collect-till line (read-line) (eof-object? line)))))

    Given:

    (define-syntax collect-till
    (syntax-rules ()
    [ (collect-till (x bag) expr test)
    (collect-till (x bag) expr test #f) ]
    [ (collect-till (x bag) expr test include-ender)
    (let go ((bag '()))
    (let ((x expr))
    (if test
    (if include-ender
    (reverse (cons x bag))
    (reverse bag))
    (go (cons x bag))))) ]
    [ (collect-till x expr test)
    (collect-till (x bag) expr test) ]
    [ (collect-till x expr test include-ender)
    (collect-till (x bag) expr test include-ender) ] ))

    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp,comp.lang.scheme on Fri Aug 22 10:37:59 2025
    From Newsgroup: comp.lang.scheme

    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 --- Synchronet 3.21a-Linux NewsLink 1.2