From: Jussi Rintanen
Subject: Variable binding in dolist
Date: 
Message-ID: <1992Jan14.133005.19105@nntp.hut.fi>
Hello everybody,

Please could someone convince me that the description of dolist in
Common Lisp : The Language (2nd edition) unambiguous! My problem with
dolist is that when I want to create closures inside a dolist and
one of the closure variables is the variable bound by dolist, I do not
get what I want.

In other words, as far as I can see, the following two macros (you just
have to imagine them completed) conform to the description in CLtL2, but
for example Allegro 4.0 works like the first one:

(defmacro dolist1 (decls &rest body)
  (let ((var (first decls))
	(listexpr (second decls))
	(resultform (third decls)))
  `(do* ((%%foo ,listexpr (rest %%foo))
	 (,var (first %%foo)(first %%foo)))
	((null %%foo) ,resultform)
	,@body)))

(defmacro dolist2 (decls &rest body)
  (let ((var (first decls))
	(listexpr (second decls))
	(resultform (third decls)))
    `(progn
       (mapc #'(lambda (,var)
		 ,@body)
	     ,listexpr)
       (let ((,var nil)) ,resultform)))) ;; var=nil in resultform (see CLtL2)

Their behaviour obviously differ as the following example shows:

(defvar list1 nil)
(defvar list2 nil)

(dolist1 (n (list 1 2 3)) (push #'(lambda () n) list1))
(dolist2 (n (list 1 2 3)) (push #'(lambda () n) list2))

where the first expression returns a list of functions returning nil,
and the second one returns a list of functions returning 1,2,3, respectively.

So, my question is, can we interpret CLtL2 (or the forthcoming ANSI Standard,
of which I have no faintest idea) in a way that rules the first or the second
one out, and, do people think that there is some reason to prefer the first
one over the second one.

Jussi Rintanen
Helsinki University of Technology
Department of Computer Science
email: ··············@hut.fi