Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate over a sequence in Common Lisp with loop?

Previously I had been using "being the elements of" feature of loop to iterate over a sequence of an unknown type. I just found out that "being the elements of" is not provided in every implementation of Common Lisp and am wondering if there is any clean way to iterate over a sequence using loop. The best solution I have been able to come with is to coerce the sequence to a list and then iterate over that.

like image 834
malisper Avatar asked Sep 17 '14 21:09

malisper


1 Answers

No, LOOP does not provide such a feature directly. If your LOOP implementation is extensible (which the standard says nothing about, too), you might be able to implement such a feature.

LOOP has clauses to iterate over lists - for item in list - and a clause to iterate over a vector - for e across vector - note that strings are also vectors, one-dimensional arrays. But not both together.

Otherwise use MAP or MAP-INTO to iterate over sequences.

The ITERATE macro provides such a feature: for i in-sequence seq.

like image 76
Rainer Joswig Avatar answered Oct 24 '22 07:10

Rainer Joswig