Just bind the variable with a copy of the value. For instance:.
(let ((ii)) (lambda () i))
This is a really important method with iterative constructs, because something like
(loop for i from 1 to 10 collecting (lambda () i))
can return ten closures to the same variables, so it becomes necessary to write:
(loop for i from 1 to 10 collecting (let ((ii)) (lambda () i)))
If you really need a function that returns a value, you can also use constantly (but I expect the real use case is more complicated):
(loop for i from 1 to 10 collecting (constantly i))
Uncertainty in the forms of iterations is in fact determined by the standard, in some cases. For example, for dotimes , dolist
This is implementation dependent, whether the dotimes sets a new var binding at each iteration or whether it sets the binding for var once at the beginning and then assigns it to any subsequent iterations.
The more primitive do , however, actually indicates that there is one set of bindings for the form and that they are updated at each iteration (highlighted by me):
At the beginning of each iteration other than the first, the vars are updated as follows. & Hellip;
This ambiguity gives implementations a bit more flexibility. A dollist , for example, can be determined using one of the following actions:
(defmacro dolist ((var list &optional result) &body body) `(progn (mapcar
(defmacro dolist ((var list &optional result) &body body) (let ((l (gensym (string '#:list-)))) `(do* ((,l ,list (rest ,l)) (,var (first ,l) (first ,l))) ((endp ,l) ,result) ,@body)))