How do you insert declarations into loops?
Example: This function loops over a list of associations
collecting the cdrs.
(defun list-cdrs (list)
(loop for (unwanted-var . wanted-var) in list
collect wanted-var))
When I compile list-cdrs, I get an "unused lexical variable, UNWANTED-VAR"
message (this is fine.). Normally, if I wanted to inhibit this
warning I would stick a (declare (ignore unwanted-var)) in the
beginning of the function body immediately following the declaration.
It doesn't seem that you can do this using loop.
Things that I tried:
(defun list-cdrs (list)
(loop for (unwanted-var . wanted-var) in list
collect
(progn (declare (ignore unwanted-var))
wanted-var)))
and (pretty obviously wrong)
(defun list-cdrs (list)
(loop for (unwanted-var . wanted-var) in list
collect wanted-var
do (declare (ignore unwanted-var))))
In both of these cases, I get an error (try this out--you find that
the declaration gets tucked into an odd spot because of the way that
the loop expansion is done...).
Mark
PS: no flames about the desirability/undesirability of the loop construct,
please.
In article <··········@elaine31.Stanford.EDU>, ·····@leland.Stanford.EDU (Mark Alan Peot) writes:
|>
|> How do you insert declarations into loops?
|>
|> Example: This function loops over a list of associations
|> collecting the cdrs.
|>
|> (defun list-cdrs (list)
|> (loop for (unwanted-var . wanted-var) in list
|> collect wanted-var))
> [..]
|> PS: no flames about the desirability/undesirability of the loop construct,
Sorry, can't resist: (mapcar #'cdr list) does what you want
I don't know the answer, but I'd suggest you macroexpand the loop,
and see if you determine where the declaration might go.
You can always just ignore it, too. Here's a possible hack,
maybe wrap a local compiler declaration saying to not given warnings. :)
--
-Kelly Murray (···@prl.ufl.edu) <a href="http://www.prl.ufl.edu">
-University of Florida Parallel Research Lab </a>
From: Barry Margolin
Subject: Re: How do you insert declarations into loops?
Date:
Message-ID: <3n8l13$5pb@tools.near.net>
In article <··········@elaine31.Stanford.EDU> ·····@leland.Stanford.EDU (Mark Alan Peot) writes:
>How do you insert declarations into loops?
In general, you can't. There is special syntax for declaring the types of
loop variables, but that's it.
> (loop for (unwanted-var . wanted-var) in list
> collect wanted-var))
>
>When I compile list-cdrs, I get an "unused lexical variable, UNWANTED-VAR"
>message (this is fine.). Normally, if I wanted to inhibit this
>warning I would stick a (declare (ignore unwanted-var)) in the
>beginning of the function body immediately following the declaration.
You can use NIL in place of a variable in a destructuring pattern to
indicate that this part of the pattern should be ignored, e.g.
(loop for (nil . wanted-var) in list ...)
--
Barry Margolin
BBN Planet Corporation, Cambridge, MA
······@bbnplanet.com
In article <··········@elaine31.Stanford.EDU>,
Mark Alan Peot <·····@leland.Stanford.EDU> wrote:
>How do you insert declarations into loops?
>Example:
> (defun list-cdrs (list)
> (loop for (unwanted-var . wanted-var) in list
> collect wanted-var))
Summary of useful responses:
In general, there is no way to insert declarations into loops.
There is a way to ignore places in a destructuring list--use 'nil'
as a place holder.
Example:
(loop for (nil . wanted-var) in list collect wanted-var)
This loop binds only "wanted-var." See CLTL2, 26.12.2.
Type declarations can be inserted into loops immediately after the
vars in "for clauses" (CLTL2 26.6). (IMHO, this is very ugly syntax.)
Examples:
(loop for a fixnum in list-of-fixnums do (print var1))
Binds the variable "a", a fixnum to successive list elements.
(loop for (var1 . var2) (fixnum . list) in assoc-list
do ...)
Binds var1 and var2, a fixnum and list respectively, to
successive associations.
Thanks to Barry Margolin, Marty Hall and pch.
Mark Peot
>PS: no flames about the desirability/undesirability of the loop construct,
>please.
These simplified examples were generated for illustrative purposes only.
Lots of folks wrote to say that you could write lisp-cdr using mapcar.
This is obvious.
In article <...> ·····@leland.Stanford.EDU (Mark Alan Peot) writes:
> How do you insert declarations into loops?
>
> Example: This function loops over a list of associations
> collecting the cdrs.
>
> (defun list-cdrs (list)
> (loop for (unwanted-var . wanted-var) in list
> collect wanted-var))
>
> When I compile list-cdrs, I get an "unused lexical variable, UNWANTED-VAR"
> message (this is fine.). Normally, if I wanted to inhibit this
> warning I would stick a (declare (ignore unwanted-var)) in the
> beginning of the function body immediately following the declaration.
> It doesn't seem that you can do this using loop.
The solution is not to introduce an ignore declartion, but instead to
use a hack in the destructuring pattern matcher:
(defun list-cdrs (list)
(loop for (NIL . wanted-var) in list
collect wanted-var))
> PS: no flames about the desirability/undesirability of the loop construct,
> please.
ditto about the gross NIL hack. :)
--
Thomas A. Russ, USC/Information Sciences Institute ···@isi.edu