I need to do a nested iteration over the same list twice, but the second loop should start where the first one is currently at.
Here's an example using a for
loop:
List l, tuple_l;
for (int x = 0; x < l.length; x++) {
for (int y = x; y < l.length; y++) {
tuple_l.push((l[x], l[y]))
}
}
And here's an example using Python's list comprehension:
[(x, y) for i, x in enumerate(l) for y in l[i:]]
I know how to do this iteration in Haskell iterating over the entire array twice...:
[(x, y) | x <- l, y <- l]
...but I'm not sure how I would go to start the second iteration on what comes after x
. Ideally, it would be able to do something like this:
[(x, y) | (x:xs) <- l, y <- xs] -- Doesn't work
Obs.: I'm aware a similar question was already answered: Haskell version of double loop
However, that approach only works for integer ranges. I guess I could somehow use that approach and access the list items by index, but that's not very haskell/functional-like.