The sequence is generated by following this pseudo-code:

A:  The first term is zero.
    Repeatedly apply:
        If the last term is *new* to the sequence so far then:
B:          The next term is zero.
        Otherwise:
C:          The next term is how far back this last term occured previously.



Example

Using A:

0
Using B:

0 0
Using C:

0 0 1
Using B:

0 0 1 0
Using C: (zero last occurred two steps back - before the one)

0 0 1 0 2
Using B:

0 0 1 0 2 0
Using C: (two last occurred two steps back - before the zero)

0 0 1 0 2 0 2 2
Using C: (two last occurred one step back)

0 0 1 0 2 0 2 2 1
Using C: (one last appeared six steps back)

0 0 1 0 2 0 2 2 1 6
...



Task

 Create a function/procedure/method/subroutine/... to generate the Van Eck sequence of numbers.
 Use it to display here, on this page:
 The first ten terms of the sequence.
 Terms 991 - to - 1000 of the sequence.


References

 Don't Know (the Van Eck Sequence) - Numberphile video.
 Wikipedia Article: Van Eck's Sequence.
  OEIS sequence: A181391.


% Generate the first N elements of the Van Eck sequence
eck = proc (n: int) returns (array[int]) 
    ai = array[int]
    e: ai := ai$fill(0, n, 0)
 
    for i: int in int$from_to(ai$low(e), ai$high(e)-1) do
        for j: int in int$from_to_by(i-1, ai$low(e), -1) do
            if e[i] = e[j] then
                e[i+1] := i-j
                break
            end
        end
    end
    return(e)
end eck
 
% Show 0..9 and 990..999
start_up = proc ()
    po: stream := stream$primary_output()
    e: array[int] := eck(1000)
 
    stream$puts(po, "  0 -   9: ")
    for i: int in int$from_to(0,9) do
        stream$putright(po, int$unparse(e[i]), 4)
    end
    stream$puts(po, "\n990 - 999: ")
    for i: int in int$from_to(990,999) do
        stream$putright(po, int$unparse(e[i]), 4)
    end
    stream$putl(po, "")
end start_up