[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Use of DO in Common Lisp Code




I looked at a spectrum of code in the Lucid implementation of Common
Lisp to see how DO is used where DOLIST and DOTIMES could not
be used. The breakdown of use of iteration is this:

	the frequency of use of DO is the same as the use of DOTIMES 
	and DOLIST combined

	the use of LABELS/FLET is used as often as DOLIST.

The uses of DO in place of DOTIMES and DOLIST are interesting. 
Sometimes the stepper is not CDR, so the use of DO is exactly
like DOLIST, but with the stepper (and end test) different; typically:

	(DO ((current data-structure (next-data current)))
	    ((empty-data current)) ...)

Sometimes the use is exactly like DOLIST, but the test is different.

	(DO ((l l (cdr l)))
	    ((end-test l)) 
	    (let ((x (car l)))
	     ...))

But the most frequent use (by a margin of 3 to 1) is a combination of
DOTIMES and DOLIST:

	(do ((l l (cdr l))
	     (i 0 (1+ i)))
	    ((null l))
	    (let ((x (car l)))
	     ...))

The use of DO outside of these paradigms is almost non-existent. Hairier
control structures are usually done with LABELS/FLET, and there are
3 large ATN's in the implementation, written with (glarg) TAGBODY.

One principle of Lisp design that DO follows is that the binding of the
iterator, its initial value, and its updator are apparent immediately upon
inspection - they are usually on one line. This principle is the basis of
(LET ((x value)) ...) being preferred to ((lambda (x ...) ...) value ...):
X and VALUE are not near each other on the page.

I don't have a proposal to make, but this data might be of use in thinking
about iteration in Scheme.

			-rpg-