Home | Contact | Bookmark Trusted Choice | Sitemap

Top Rated Articles

Custom Class Ring, customizing LOOP for a custom class ?




I generally find the LOOP constructs more readable than
various DO constructs or MAP-this-and-that with LAMBDA.
However, sometimes i need to iterate in some way over
a custom object.

Python has the ability to define what the FOR loop does
with an instance of a custom class. This does not seem
soooo difficult to implement in CL. I'd try to do
it myself, but i'd perfer to use a standard mechanism if
there is one.
If you have the misfortune of using a Common Lisp system which does
not document or ship with the extension interface, download an
uncrippled LOOP such as GSB LOOP or the publicly available Symbolics
LOOP and read up on its extension protocol. With just a few lines of
code you can start writing definitions like

(defun vertex-list (object)
(loop for vertex being the vertices of object
collect vertex))


The excellent Symbolics LOOP code is still available at the CMU AI
repository. If you poke around the test code which comes with the
sources you should find examples of inclusive LOOP paths and other
such winnage the purists wouldn't dare tell you about.


Many implementations of LOOP have a way to extend the iteration pathways
available, but this feature is not standardized. SERIES is a comparable
system to LOOP, but allows for easy extension. You express SERIES
operations as lazy sequence operations and then, if possible, the SERIES
library will convert that to an iterative construct. Adding your own
SERIES operator is a matter of a simple DEFUN with a declaration of how
it is to behave when SERIES tries to compile it as part of an iteration.
Documentation is available in the appendices of CLtL2. A maintained
implementation is available at

Other Articles