| This isn't meant to reopen the great LOOP flame war but one way of
| doing it is:
|
| (with-open-file (ifile "key.html" :direction :input)
| (loop for line = (read-line ifile nil)
| while line
| counting 1))
Simply using while (read-line ifile nil nil) seems even more compact.
However, I have this negative gut reaction to wanton waste, as in
effectively allocating as many strings as there are lines for no
good reason, just exercising the garbage collector, so while we're
at it, how about
(loop for char = (read-char ifile nil nil)
while char
count (char= char #\newline))
Note that read-line will return nil on an empty file, but the data
leading up to the end-of-file if no newline intervened and then nil
on the next call, effectively counting a non-empty file containing
no newlines as having one line. This may be relevant.
| (loop for line being each line of ifile
| count 1)
Elegant.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 403 |
Nodes: | 16 (2 / 14) |
Uptime: | 43:20:07 |
Calls: | 8,407 |
Calls today: | 2 |
Files: | 13,171 |
Messages: | 5,905,019 |