I looked up the related answers, it must have to do with the scope. Toy example similar to mine in structure:
foo[i_] := Array [If[Slot[1] == Slot[i + 1], 0, 1] &, ConstantArray[2, 3]];
Fail: in bar[1], Slot[1+1] does not compute. I hotfixed it by using
List[##][[i+1]]
instead of the 2nd Slot and it works as intended. Of course I can apply generous Where, With etc. statements instead like suggested in the other answers, but I'm just curious: When does Slot goes on Hold? Do other constructs than Slot have the same problem?
With
to make surei+1
gets evaluated before enteringSlot
:foo[i_] := With[{j = i + 1}, Array[If[Slot[1] == Slot[j], 0, 1] &, ConstantArray[2,3]] ];
$\endgroup$With
. Still, for the benefit of readers who don't know how to useWith
. $\endgroup$