Is there an efficient (i.e. built-in) way to return every other element
from a list? Suppose I have:
'(6 9 14 18 22 26 31)
and I want:
'(6 14 22 31)
This looks like a great case for MAPCAR, but I'm not exactly sure how to
accomplish it. Anyone??
------------->
Kevin Goodier
····@cec.wustl.edu
http://students.cec.wustl.edu/~bkg2/
In article <·························@newsreader.wustl.edu>,
Kevin Goodier <····@cec.wustl.edu> wrote:
>Is there an efficient (i.e. built-in) way to return every other element
>from a list? Suppose I have:
>
> '(6 9 14 18 22 26 31)
>
>and I want:
>
> '(6 14 22 31)
(loop for x in list by #'cddr
collect x)
This example is on the bottom of p.720 of CLtL2.
--
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Cambridge, MA
Support the anti-spam movement; see <http://www.cauce.org/>
Please don't send technical questions directly to me, post them to newsgroups.
In article <·························@newsreader.wustl.edu>,
····@cec.wustl.edu (Kevin Goodier) wrote:
> Is there an efficient (i.e. built-in) way to return every other element
> from a list? Suppose I have:
>
> '(6 9 14 18 22 26 31)
>
> and I want:
>
> '(6 14 22 31)
>
> This looks like a great case for MAPCAR, but I'm not exactly sure how to
> accomplish it. Anyone??
(loop for item in '(6 9 14 18 22 26 31) by #'cddr
collect item)
(labels ((every-other (list)
(if (null list)
nil
(cons (first list)
(every-other (rest (rest list)))))))
(every-other '(6 9 14 18 22 26 31)))
--
http://www.lavielle.com/~joswig/