Initializing an array of relative coordinates of all adjacent 2D matrix cells The program should output this:

_1 _1
_1  0
_1  1
 0 _1
 0  1
 1 _1
 1  0
 1  1


Currently I have this code:

(4 ~: i.9) # (3 # i:1) ,. 9 $ i:1


But I think it can be better.
Your solution relies excessively on magic numbers, such as 9 and 3.  It also obscures the Cartesian-product aspect of the problem.  Therefore, I think it could be better written as:

(&lt; (&lt; (&lt; 4))) { ,/ &gt; { ;~i:1


Derivation


(i:1) produces the list _1 0 1.
{ (i:1) ; (i:1) produces the Cartesian product:

┌─────┬────┬────┐
│_1 _1│_1 0│_1 1│
├─────┼────┼────┤
│0 _1 │0 0 │0 1 │
├─────┼────┼────┤
│1 _1 │1 0 │1 1 │
└─────┴────┴────┘


@earl points out that this reflexive expression can be written more succinctly as {;~i:1.
&gt; unboxes it into a 3 × 3 × 2 matrix, and ,/ flattens it into a 9 × 2 matrix:

_1 _1
_1  0
_1  1
 0 _1
 0  0
 0  1
 1 _1
 1  0
 1  1

The only remaining task is to exclude the 0 0 row, which is element 4.  The (&lt; (&lt; (&lt;4))) { selector seems to do this job.