B. Pym
2024-09-11 03:54:51 UTC
(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(with-open-file (strm "/tmp/test.big")
(loop as line = (read-line strm nil nil)
while line
summing (length line))))
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