Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Haskell

Idiom #42 Continue outer loop

Print each item v of list a which is not contained in list b.
For this, write an outer loop to iterate on a and an inner loop to iterate on b.

Control flow jumping back to the beginning of the outermost loop
sequence_ [ print v | v <- a, [ u | u <- b, u == v] == [] ]

a _\\ b abbreviates this task if you are not required to write your own printing iterations

Our outer loop continues to the next element whenever the inner list equality test breaks the inner iteration lazily after the first of any [u|u==w] found growing too large to equal [].
int *v = a;
while (v < a+N)
{
    int *w = b;
    while (w < b+M)
    {
        if (*v == *w)
            goto OUTER;
        
        ++w;
    }
    printf("%d\n", *v);
    
    OUTER: ++v;
}

N is the length of a.
M is the length of b.

Using goto is usually considered bad practice in C.

New implementation...
< >
programming-idioms.org