I am defining a pattern to be used as a test, that itself depends on an argument. For instance, I might want to check that something appears in a given list:
type[x_] := (MemberQ[x, #] &);
I naively used this in the definition of a function and do not get what I expected. Defining
Z2 = {0, 1};
f[x_?type[Z2]] := x
returns f[0] when evaluating f[0]. Putting brackets
g[x_?(type[Z2])] := x
gives the desired 0 when evaluating g[0].
I understand the need for brackets here, as one might want to pattern test an object and then supply it an argument -- (f_?test)[x] -- and I do not mean to get into a discussion about the choice made by Mathematica here.
My question is whether there is a way to define the pattern test "type", so as to avoid the need for subsequent brackets.
type[x_,y_]:=MemberQ[x,y]
and thenf[x/;type[Z2,x]]:=x
. $\endgroup$t=type[Z2]; f[x_?t] := x
$\endgroup$