Let Aand Bbe lists. I had to find all the pairs {x,y}for which I am xin A, yis in B, and some condition is Cond[x,y]true. This is what I came up with, but its rather cumbersome, and I suspect there is a better way
AllPairs[A_, B_, Cond_] := Module[{i, k, C, Cp},
C = {};
For[i = 1, i <= Length[A], i++,
Cp = Select[B, Cond[A[[i]],
C = C~Join~Table[{A[[i]], Cp[[k]]}, {k, 1, Length[Cp]}];
];
Return[C];
]
for instance
In[1]:= AllPairs[{1, 2, 3, 4}, {3, 4, 5}, EvenQ[#1 + #2] &]
Out[1]:= {{1, 3}, {1, 5}, {2, 4}, {3, 3}, {3, 5}, {4, 4}}
My other problem with this code is that it is not easy to generalize. I would like to have a function that accepts lists A1, A2,...,Anand some condition Cond[x___]and prints all n tuples {x1,x2,...,xn}for which it x1is in A1... xnis in Anand is Cond[x1,x2,...,xn]true.
, , , ?
!