Example
The following is a typical example of the usage of list comprehension:
⇒
Implementation
The macro collect-list
can be constructed using with-collect
macro and LOOP (or iterate) macro. Thus it can collect data in the data types which are supported by LOOP and Iterate. In fact, using with-collect
and loop
makes the implementation quite easy and straightforward.
Here's the implementation of collect-list (based on Loop):
And the example will be expanded into:
Which is also very efficient.
The with-collect
macro can be found in CLOCC/CLLIB/simple.lisp.
Other Examples
Other Implementations
For another take on comprehensions see List comprehensions for Lisp, an LGPL'd general collect macro.An afterthought
It’ll be handy to use Iterate instead of Loop, in order to take advantage of its flexibility and expressiveness. But some forms like (for y previous x) are something you definitely don’t want to nest. One way to identify that is to construct it as ((for x …) (for y …)).
macro example