The following is a typical example of the usage of list comprehension:
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
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.
with-collect macro can be found in CLOCC/CLLIB/simple.lisp.
Other ImplementationsFor another take on comprehensions see List comprehensions for Lisp, an LGPL'd general collect macro.
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 …)).