Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!mailrus!ames!zodiac!joyce!sri-unix!quintus!ok
From: ok@quintus.uucp (Richard A. O'Keefe)
Newsgroups: comp.lang.lisp
Subject: Re: DOLIST as DO
Message-ID: <169@quintus.UUCP>
Date: 15 Jul 88 22:08:39 GMT
References: <327@soi.UUCP> <228@esosun.UUCP>
Sender: news@quintus.UUCP
Reply-To: ok@quintus.UUCP (Richard A. O'Keefe)
Organization: Quintus Computer Systems, Inc.
Lines: 22

In article <228@esosun.UUCP> jackson@esosun.UUCP (Jerry Jackson) writes:
>In article <327@soi.UUCP> alex@soi.UUCP (Alex Zatsman) writes:
>   Anybody knows an elegant way of representing DOLIST construct
>   through DO ?  How about DO* ?
>How about...
>
>(defmacro dolist ((var list &optional result-form) &rest body)
>  (let ((listvar (gensym)))
>    `(do* ((,listvar ,list (cdr ,listvar))
>	  (,var (car ,list) (car ,listvar)))
>	 ((null ,listvar) ,result-form)
>	 ,@body)))

That's not going to work too well when the list happens to be empty,
is it?  A small change fixes that:

(defmacro dolist ((var list &optional result-form) &rest body)
    (let ((listvar (gensym)))
       `(do ((,listvar ,list (cdr ,listvar)))
	    ((null ,listvar) ,result-form)
	    (let ((,var (car ,listvar)))
		,@body))))