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

1

8

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.

Jilolo answered 17/9, 2014 at 21:33 Comment(0)
A
12

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.

Antiphrasis answered 17/9, 2014 at 21:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.