Subject: Re: lisp idiom for processing each line in a file?
From: (Rob Warnock)
Date: Mon, 20 Feb 2006 20:25:52 -0600
Newsgroups: comp.lang.lisp
Message-ID: <>
David Trudgett  <> wrote:
| Your suggestion is OK if you like that sort of thing, but I
| have a natural aversion to using arbitrary values as booleans...

But remember, this is Common Lisp, not Scheme. In CL the *only*
distinguished boolean is NIL as "false" -- *everything* else is "true".
Plus, we're using (READ-LINE stream NIL NIL) here, where the outcome is
*guaranteed* [absent an I/O error throwing an exception] to be either
a [possibly zero-length] string ["true"] or NIL. So you've already got
an unambiguous boolean result, and thus (WHEN LINE ...) is IMHO a more
natural way than (UNLESS (NULL LINE) ...) to say what you're really
trying to do.

The same is true of READ-CHAR but *not* of READ, which can return NIL,
which is why people tend to use the following variation when using READ:

    (with-open-file (stream "filename")
      (loop with eof = (list nil)
	    for form = (read stream nil eof)
	    until (eq form eof) do
        (process-one-form form)))

And of course some prefer to use the "WHILE (NOT (EQ FORM EOF))"
variant here, for parallelism with other LOOPs that use WHILE.


Rob Warnock			<>
627 26th Avenue			<URL:>
San Mateo, CA 94403		(650)572-2607