Subject: Re: Lisp newbie, data structures, parsing.
From: rpw3@rpw3.org (Rob Warnock)
Date: Tue, 03 Jan 2006 23:06:08 -0600
Newsgroups: comp.lang.lisp
Message-ID: <Wr6dnYOAD4tdyibeRVn-sg@speakeasy.net>
Liam Clarke <ml.cyresse@gmail.com> wrote:
+---------------
| I now know what (token (read-token) (read-token)) means,
| (variable initial-value incr-by-value).
+---------------

Small quibble: It's actually (variable initial-value subsequent-values),
since there may be no notion of "incrementing" going on. Or as the CLHS
says it:

    (var [init-form [step-form]])

Notice that for DO and DO* a missing step-form simply leaves the
variable unmodified the next time through, as if the "step-form"
were simply "var". [Though it might be modified by explicit assignments
the programmer does in the body.]

[You're probably not ready for this yet, but when you are...]

This should be contrasted with the behaviour of the LOOP macro's
"for-as-equals-then" variant:

    (LOOP FOR var = [form1 [THEN form2] ...)

where if "form2" is omitted then the variable "var" still *is* updated
every time through the loop, but by the value of "form1", which is
re-evaluated every time though the loop. That's why the common idiom
for stepping through the lines of a file is this:

    (with-open-file (s "filename")
      (loop for line = (read-line s nil nil)
	    while line
	do (process-one-line line)))

instead of this:

    (with-open-file (s "filename")
      (loop for line = (read-line s nil nil) then (read-line s nil nil)
	    while line
	do (process-one-line line)))

whereas using DO would require repeating the call to READ-LINE:

    (with-open-file (s "filename")
      (do ((line (read-line s nil nil) (read-line s nil nil)))
	  ((null line))
	(process-one-line line)))

However, there's even an idiom which shortens that a little, too:  ;-}

    (with-open-file (s "filename")
      (do ((line #1=(read-line s nil nil) #1#))
	  ((null line))
	(process-one-line line)))


-Rob

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607