Pascal J. Bourguignon <pjb@informatimago.com> wrote:
+---------------
| Mark Wooding <mdw@distorted.org.uk> writes:
| > rpw3@rpw3.org (Rob Warnock) writes:
| >> Writing "EOF = (LIST)" or "EOF = (CONS NIL NIL)" [but the former is
| >> shorter!] would have worked just as well, and then EOF would have
| >> contained "(NIL)".
| >
| > Umm. (LIST) evaluates to NIL, which isn't particularly useful.
| > (LIST NIL) is still shorter than (CONS NIL NIL), though.
+---------------
*D'Oh!* Of course Mark is correct here! I must have been having a
momentary brain fart when I said "(LIST) ==> (NIL)". Sorry 'bout that.
(LIST NIL) or (LIST :EOF) it is, then.
[One could always use the shorter (LIST 0) or (VECTOR), but that would
just be silly, not to mention very confusing to anyone reading your code.]
+---------------
| Well if shorter is what you want, notice that the reader won't be able
| (unless you extend yourself extraordinarily, but the same could be
| done with the value bound to eof) to read the stream itself, so you
| could as well write:
|
| (loop with eof = stream
| for item = (read stream nil eof)
| until (eq eof item)
| collect item)
+---------------
Nice try! And I almost fell for it, too! ;-} ;-} Unfortunately,
STREAM here is only required to be an "input stream designator"
and not an actual stream, so if STREAM is being passed in from
some outer context it might well have the value T [denoting the
value of *TERMINAL-IO*] or NIL [denoting the value of *STANDARD-INPUT*],
and the chance of a T or NIL being in random sexp input is far
too high for this to be considered safe.
Probably the best thing for production code is:
(defconstant *eof* (load-time-value (list :eof)))
but an inlined (LIST :EOF) will suffice for one-off & examples.
-Rob
-----
Rob Warnock <rpw3@rpw3.org>
627 26th Avenue <URL:http://rpw3.org/>
San Mateo, CA 94403 (650)572-2607