 # Finding, comparing, and positioning in nested loop

IntervalVectors-PlainText.txt (12.8 KB) I’m working on a patch to find the pitch class sets that correspond to particular interval vectors. Attached is a table of lists, which takes the form of:

((Interval vector) (corresponding set) (optional: a second corresponding set))

This table will then be encapsulated in a list, as follows:

(
((Interval vector) (corresponding set) (optional: a second corresponding set))
((Interval vector) (corresponding set) (optional: a second corresponding set))
((Interval vector) (corresponding set) (optional: a second corresponding set))

)

I want to be able to choose an interval vector (which is the first element in each 2 or 3 element list of interval vectors) and retrieve the corresponding set(s). I used mapcar and first to produce a list of the interval vectors (each of which is a list) but I couldn’t figure out how to compare a list of my-interval-vector to the nested list of all interval vectors to see if it’s there and retrieve its position).

In other words, first and mapcar yield:

((Interval vector) (Interval vector) (Interval vector) …)

then I want to see if (my-interval-vector) is in that list and what its position is so I can retrieve the second and possibly third elements of: ((Interval vector) (corresponding set) (optional: a second corresponding set))

(I hope I’ve been clear. LOL)

Don

PS: the table is aligned with all of the Forte interval vectors → sets so if I get this patch working I’ll be pleased to share it with everyone.

Hi! Sorry I just see this message. I’m not sure I understand what you need, but:

I want to see if (my-interval-vector) is in that list and what its position is so I can retrieve the second and possibly third elements

=> The Lisp function `find` is probably a good start: using the `:key` argument you can tell it what to test, so you could do something like:

``````(find my-interval-vector my-big-table :key 'first)
``````

this would return the whole element found in the table, and you can get the rest of it with `cdr`:

``````(cdr (find my-interval-vector my-big-table :key 'first))
``````

and if you really need to know the position in the list for something else, you can do the same with `position`:

``````(position my-interval-vector my-big-table :key 'first)
``````