[comp.lang.lisp] Style sheets

phil@Neon.Stanford.EDU (Phil Stubblefield) (02/14/91)

In article <4121@skye.ed.ac.uk> jeff@aiai.UUCP (Jeff Dalton) writes:
>In article <1991Feb12.122415.23035@src.dec.com> meehan@src.dec.com
 (Jim Meehan) writes:
>>Of course, having the wealth of choices for writing the same code is
>>not always a good thing.  We had an in-house "Common Lisp style sheet"
>
>This is a good idea.  Would it be possible to have a copy?  I'd
>be interested in seeing what you decided.


I'd also be interested in seeing a copy.  I'd like to know the opinion
of "mature Lisp programmers" ;] on various constructs.  For example,
what's the stylistic complaint with DO*?  I sometimes use it in the
following manner:

(do* ((plist initargs (cddr plist))
      (keyword (first plist) (first plist))
      (value (second plist) (second plist)))
     ((null plist))
  (when (and (tricky-option-p keyword)
	     (humongous-value-p value))
    (frob keyword value)))

One alternative would be:

(do ((plist initargs (cddr plist)))
    ((null plist))
  (let ((keyword (first plist))
	(value (second plist)))
    (when (and (tricky-option-p keyword)
	       (humongous-value-p value))
      (frob keyword value))))

It seems to me that in the first version, the two lexical variables
KEYWORD and VALUE are allocated in the loop prologue and released in
the epilogue, while in the second version the allocation/release
occurs every single iteration.  I can hear it now: "Let the compiler
do the optimizing, Stubblefield!"

Another alternative is:

(do ((plist initargs (cddr plist))
     keyword
     value)
    ((null plist))
  (setq keyword (first plist))
  (setq value (second plist))
  (when (and (tricky-option-p keyword)
	     (humongous-value-p value))
    (frob keyword value)))

But if these variables are allocated by DO, why not use it to set
them?  (Especially since (CDR NIL) is NIL, etc.?)

Yet another alternative would be to avoid the additional variables
altogether by using (FIRST PLIST) and (SECOND PLIST) throughout, but
that seems much less readable if the values must be used in many
places.

And for those into ANSI LOOPing, the alternative is quite succinct:

(loop for (keyword value) on plist by #'cddr
      do
  (when (and (tricky-option-p keyword)
	     (humongous-value-p value))
    (frob keyword value)))

So what are the pros and cons, folks?
-- 

Phil Stubblefield                                        (415) 325-7165
Rockwell Palo Alto Laboratory                    phil@rpal.rockwell.com