Discussion:
When does ADJUST-ARRAY cons?
(too old to reply)
B. Pym
2024-09-11 03:54:51 UTC
Permalink
(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
Kaz Kylheku
2024-09-11 04:29:29 UTC
Permalink
Post by B. Pym
Gauche Scheme
(use gauche.generator)
(define (foo f)
(generator-fold
+ 0
(gmap string-length (file->generator f read-line))))
(foo "output.dat")
===>
119
Yagoddabekidding!

1> (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: @***@mstdn.ca
B. Pym
2024-09-11 09:16:15 UTC
Permalink
Post by B. Pym
(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)."

Loading...