Subject: Re: Idiom for gathering pairs from a list?
From: rpw3@rpw3.org (Rob Warnock)
Date: Mon, 23 Oct 2006 22:45:21 -0500
Newsgroups: comp.lang.lisp
Message-ID: <2-6dnZNJeMTMEaDYnZ2dnUVZ_qKdnZ2d@speakeasy.net>
Pascal Bourguignon  <pjb@informatimago.com> wrote:
+---------------
| "bradb" <brad.beveridge@gmail.com> writes:
| > Zach Beane wrote:
| >> "bradb" <brad.beveridge@gmail.com> writes:
| >> > Is there a nicer way to gather the pairs than that?
| >> LOOP destructures. Try this:
| >>    (loop for (start end) on (all-matches ...) by #'cddr do ...)
| > Ah, that is much much nicer.  It is a shame that loop doesn't defualt
| > to looping by the natural size of the destructure, so you don't need
| > the BY #'CDDR.
| 
| (defmacro loop* (&rest stuff)
|    ...
|    :for ,destructuring-stuff :on ,list by (lambda (list)
|                                            (nth-cdr ,(length
| destructuring-stuff)
+---------------

The problem with any such automatic sizing is LOOPs like this:

    (loop for (a b c . rest) on list by #'cdddr ...)

or even this:

    (flet ((stepper (list)
	     (case (car list)
	       ((:one)   (cddr list))
	       ((:two)   (cdddr list))
	       ((:three) (cdddr list))
	       (otherwise (cdr list)))))
      (loop for (key a b c) on list by #'stepper ...))

which can parse lists like this:

    (:one 123 :two 234 453 :three 7 5 8 :special :other 99 22 :two 34 54)


-Rob

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607