• Re: When does ADJUST-ARRAY cons?

    From B. Pym@21:1/5 to Edi Weitz on Wed Sep 11 03:54:51 2024
    XPost: comp.lang.scheme

    Edi Weitz wrote:

    (defun foo ()
    (with-open-file (strm "/tmp/test.big")
    (loop as line = (read-line strm nil nil)
    while line
    summing (length line))))

    Bad. Very bad. Fails to remove any carriage return
    at the end of the line. Consequently, the sum
    may be incorrect.


    Instead of CL, let's use a Lispy language.

    Sum the lengths of all of the lines in a text file.
    The length of a line is measured after removing the
    end-of-line characters at the end.

    Gauche Scheme

    (use gauche.generator)

    (define (foo f)
    (generator-fold
    + 0
    (gmap string-length (file->generator f read-line))))

    (foo "output.dat")
    ===>
    119

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to B. Pym on Wed Sep 11 04:29:29 2024
    XPost: comp.lang.scheme

    On 2024-09-11, B. Pym <Nobody447095@here-nor-there.org> wrote:
    Gauche Scheme

    (use gauche.generator)

    (define (foo f)
    (generator-fold
    + 0
    (gmap string-length (file->generator f read-line))))

    (foo "output.dat")

    119

    Yagoddabekidding!

    (lflow "/etc/hosts"
    file-get-lines
    (sum len))
    213

    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @Kazinator@mstdn.ca

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to B. Pym on Wed Sep 11 09:16:15 2024
    XPost: comp.lang.scheme

    B. Pym wrote:

    Edi Weitz wrote:

    (defun foo ()
    (with-open-file (strm "/tmp/test.big")
    (loop as line = (read-line strm nil nil)
    while line
    summing (length line))))

    Bad. Very bad. Fails to remove any carriage return
    at the end of the line. Consequently, the sum
    may be incorrect.


    Instead of CL, let's use a Lispy language.

    Sum the lengths of all of the lines in a text file.
    The length of a line is measured after removing the
    end-of-line characters at the end.

    Gauche Scheme

    (use gauche.generator)

    (define (foo f)
    (generator-fold
    + 0
    (gmap string-length (file->generator f read-line))))

    (foo "output.dat")
    ===>
    119

    The "hyperspec" says:

    "The primary value, line, is the line that is read, represented
    as a string (without the trailing newline, if any)."

    No mention is made of the carriage-return. My testing with
    SBCL proves that the CR isn't removed.

    Gauche Scheme:

    "Reads one line (a sequence of characters terminated by
    newline or EOF) and returns a string. The terminating newline
    is not included. This function recognizes popular line
    terminators (LF only, CRLF, and CR only)."

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)