## Can this function be written without the loop?

Learning APL or new to Dyalog? Ask "silly" questions here, without fear...

### Can this function be written without the loop?

This function produces the difference table of a boolean vector:

y←fanion x;n
n←⍴x
y←⍳0
:For n :In ⍳n
y←y,⊂x
x←(1↓x)≠¯1↓x
:EndFor

Can it be written without the For loop?

The result is usually displayed as a triangular structure in which each item in a row is the ≠ (exclusive-or) of the two items immediately above it:

fandisp x;n;i;⎕IO
⎕IO←1
n←⍴x
x←⍉(1,n)⍴x
⎕←' ',x[1;1]
i←2
:While i≤n
⎕←(i↑' '),x[i;1]
i←i+1
:EndWhile
Stu

Posts: 97
Joined: Thu Dec 31, 2015 1:30 am

### Re: Can this function be written without the loop?

Hmm...
what about replacing the whole function with a simple d-fun:
Code: Select all
`⍬{0∊⍴⍵:⍺ ⋄ (⍺,⊂⍵)∇ 2≠/⍵}`

-Veli-Matti
Veli-Matti

Posts: 56
Joined: Sat Nov 28, 2009 3:12 pm

### Re: Can this function be written without the loop?

This is really nice: ⍬{0∊⍴⍵:⍺ ⋄ (⍺,⊂⍵)∇ 2≠/⍵}, but I don't completely understand the recursive step. How does 2≠/⍵ form the first difference of ⍵?
Stu

Posts: 97
Joined: Thu Dec 31, 2015 1:30 am

### Re: Can this function be written without the loop?

See help for N-wise reduction:

X [the left argument] can be thought of as the width of a ‘window’ which moves along vectors drawn from the Kth axis of Y. [the right argument]
and
If X is negative, each sub-vector is reversed before being reduced.

which with X=2 produces the same result as your
`      (1↓x)≠¯1↓x`

In fact yours is actually equivalent to
`      ¯2≠/x`
or
`      2≠⍨/x`
as
`      2≠/x`
would be equivalent to
`      (¯1↓x)≠1↓x`
comparing the first with the second, second with third &c. while yours compares second with first, third with second usw. As is commutable it makes no difference in this case.

The recursion is being applied between the result so far and the amended x.

Phil Last

Posts: 557
Joined: Thu Jun 18, 2009 6:29 pm

### Re: Can this function be written without the loop?

`      fanion←{{⍵,⊂2≠/⊃⌽⍵}⍣(¯1+≢⍵)⊂⍵}      ⊢ b ← 1=?7⍴20 0 1 1 1 0 1      fanion b┌─────────────┬───────────┬─────────┬───────┬─────┬───┬─┐│0 0 1 1 1 0 1│0 1 0 0 1 1│1 1 0 1 0│0 1 1 1│1 0 0│1 0│1│└─────────────┴───────────┴─────────┴───────┴─────┴───┴─┘`
Roger|Dyalog

Posts: 215
Joined: Thu Jul 28, 2011 10:53 am

### Re: Can this function be written without the loop?

`      fanion←{{⍵,⊂2≠/⊃⌽⍵}⍣(¯1+≢⍵)⊂⍵}`

At this point allow me put in a good word for extending the power operator to allow a non-scalar "exponent" (right operand), modelled as follows:

`      powop←{⍺←⊣ ⋄ ⍺∘⍺⍺{⍺⍺⍣⍵⊢⍵⍵}⍵¨⍵⍵}`

With that, the fanion function can be:

`      fan1←{2 ≠/powop(⍳≢⍵) ⊢⍵}    ⍝ fan1←{2 ≠/⍣(⍳≢⍵) ⊢⍵}      b0 0 1 1 1 0 1      fan1 b┌─────────────┬───────────┬─────────┬───────┬─────┬───┬─┐│0 0 1 1 1 0 1│0 1 0 0 1 1│1 1 0 1 0│0 1 1 1│1 0 0│1 0│1│└─────────────┴───────────┴─────────┴───────┴─────┴───┴─┘      fanion b┌─────────────┬───────────┬─────────┬───────┬─────┬───┬─┐│0 0 1 1 1 0 1│0 1 0 0 1 1│1 1 0 1 0│0 1 1 1│1 0 0│1 0│1│└─────────────┴───────────┴─────────┴───────┴─────┴───┴─┘`

For fan1, you need the appropriate modifications if ⎕io is not 0.
Roger|Dyalog

Posts: 215
Joined: Thu Jul 28, 2011 10:53 am

### Re: Can this function be written without the loop?

Thanks to everyone who responded to my question. I'm impressed, again, at the power of the new capabilities incorporated into APL since I learned the original version of the language in the 1970's.
Stu

Posts: 97
Joined: Thu Dec 31, 2015 1:30 am