Why does the scan not stop ?

f:{$[y<>0;x | y;y]} /dyad

f2:{x:(f) x;x:(|)x} /monad

f1:{f2;f2} /monad

l: 8 9 8 0 6 7 6

(f1) l     —> Why does it run forever when the 2nd and 4th elements of the scan are the same ?  I thought fmonad scan stops on last seen or O/P same as argument

(f1)[10; l]

Output:

(8 9 8 0 6 7 6j;7 7 6 0 9 9 8j;9 9 9 0 7 7 7j;7 7 7 0 9 9 9j;9 9 9 0 7 7 7j;7 7 7 0 9 9 9j;9 9 9 0 7 7 7j;7 7 7 0 9 9 9j;9 9 9 0 7 7 7j;7 7 7 0 9 9 9j;9 9 9 0 7 7 7j)

.z.k

2016.10.10

.z.K

3.4

Hi,

Scan only stops when the current output is the same as the previous output of the function, not just any previous output. In this case if 3rd and 4th outputs were the same it would stop.

If you need to stop when any previous output is seen then you will need to store each output and check if the most recent output is in that list.

Here is an example of that:

q){if[not x[1]~distinct x 1;:x];x[1],:enlist x[0]:f2[x 0];x}/[(l;enlist l)]

9 9 9 0 7 7 7

(8 9 8 0 6 7 6;7 7 6 0 9 9 8;9 9 9 0 7 7 7;7 7 7 0 9 9 9;9 9 9 0 7 7 7)

Regards,

Thomas Smyth

AquaQ Analytics

Apart from matching previous output, monadic scan/over also stops/converges if current output equals initial input.

Using your function f2, there is another way to achieve the same thing

q){x[1]~distinct x 1}{x[1],:x[0]:f2[x 0];x}/(l;enlist l)9 9 9 0 7 7 7(8 9 8 0 6 7 6;7 7 6 0 9 9 8;9 9 9 0 7 7 7;7 7 7 0 9 9 9;9 9 9 0 7 7 7)

can simplify it

{x~distinct x}{x,enlist f2[last x]}/enlist l

in k

|{x~?x}{(,f2@*x),x}/,l