# Nested loop help

Attached is a patch which works correctly to calculated the interval between the first element in a set and the successive elements. (Even if there is a much more concise way of doing this, let’s live with this to address the issue.)

I’d now like to iterate this patch so that it makes the same calculation from each successive element. For example, the next iteration would produce (1 2 4 5 7 9 ), that results from the calculations 3 minus 2, 4 minus 3, etc.

How do I do that? Thanks!

PS: If you have a really elegant alternative to the patch, I’d love to see it… but I do want to understand how to do this nested loop. I realize it could involve “rest” but I can’t figure it out.

working loops 7.opat (7.1 KB)

Doesn’t this just amount to subtracting the first elements of the list to the rest of the list ?
I think `OM-` would allow you to do that in 1 box

``````(om- (2 3 4 6 7 9 11) 0) => (2 3 4 6 7 9 11)
(om- (3 4 6 7 9 11) 2) => (1 2 4 5 7 9)
...
``````

In other words, for a list `L`:

``````(om- (cdr L) (car L))
``````

Once you have figured how to do it for one, applying to each element can be done either by embedding this in a loop (or in another level of loop if you want to keep your current implementation of it) or in a recursion so that the same process applies each time to the next tail.

The `loop-tail` iterator is probably a good start for doing that: it takes each time the next “tail” of the list.

Thanks so much. I’ll try this approach.

This was something that was conceptually trivial but has given me all kinds of frustration to get right! LOL

Et voilà, ça marche !

interval-vector.opat (6.8 KB) interval-vector-test.opat (2.4 KB) pcset-intervals.opat (6.4 KB) Merci Jean.

1 Like

Salut,

pc2complementary-pc is missing

merci

Jerome

Here you go.pc2complementary-pc.opat (5.3 KB)

thank you very much Das