Loop extensibility
Sunday, October 3, 2004
A few days ago, Zach
pointed to the announcement on c.l.l. of a new implementation of
Loop. The new Loop implementation code was interesting in itself, as
was the
Sacla project which it is part of (Sacla is an attempt to create a
complete implemenation of CL in CL). However, I've been reading the
discussion on c.l.l. which followed the initial announcement, and the
thread has some interesting comments on how to extend Loop.
Carl Shapiro
noted that Lucid had documented writing extensions to the Loop
macro in their
Loop Facility manual. Unfortunately, the extensibility mechanism
does not appear to be present in all CLs today; however, it does
appear to exist (and I did manage
to test it) in
LispWorks. This
User extension mechanism allows a programmer to extend the
predefined loop itereation capabilities by using loop methods. For
example, the following types of extensions are possible:
- Use any loop iteration control keyword or loop method name to
initiate loop processing:
CL-USER> (define-loop-macro for) FOR CL-USER> (for x in '(1 2 3 4) by #'cddr collect x) (1 3)
- Define a custom mechanism for iterating over a sequence. For
example, to iterate over a simple string:
CL-USER> (defloop (schar schars) schar length simple-string string-char) (SCHAR SCHARS) CL-USER> (loop for c being each schar of "Temp String" count c) 11 instead of writing: CL-USER> (loop for c being each element of (the simple-string "Temp String") count c) 11
Since the Loop Facility's extensibility mechanism doesn't seem to be in the ANSI standard Loop Facility definition and is not in all CL implementations of Loop, I'm not sure whether Loop extensibility is something that is only present in Xanalys LispWorks or whether there are equivalents in other CL's.

