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

Re: How's that again?



On Tue Feb  1, Bob Hall writes:
> Is the following behavior a bug, or am I blowing the declaration somehow?

> ? (defun foo (l)
>     (dolist (x l)
>       (print "Hi")))
> ;Compiler warnings :
> ;   Unused lexical variable X, in FOO inside an anonymous lambda form.
> FOO
> ? (defun foo (l)
>     (dolist (x l)
>       (declare (ignore x))
>       (print "Hi")))
> ;Compiler warnings :
> ;   Variable X not ignored, in FOO inside an anonymous lambda form.
> FOO

The first error is correct since the variable x is declared in the 
dolist form but not used. The second error is also correct since the
variable x is used for the "dolist" iteration but not in the body.

The following removes the error message:
? (defun foo (l)
    (dolist (x l)
      (declare (ignorable x))
      (print "Hi")))
FOO

Some stylistic remarks are in order.

Unless you intend to reference x in the body of the dolist form, you
should use dotimes or loop.

(defun foo (list)
  (loop for i from 1 to (length list)
        do (print "Hi")))

Also, please choose variable names that mean something. For example,
use "list"  instead of "l" and "el" (or "element") instead of "x".


mark