# Brackets on pattern test with argument

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 when evaluating f. Putting brackets

g[x_?(type[Z2])] := x


gives the desired 0 when evaluating g.

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.

• Question unclear to me. Could you please add an example of what are your starting data and of what you want to get? Feb 27 '20 at 0:45
• In terms of "starting data", everything is defined in the question. What I am hoping to get is a definition of the function "type", so that the definition f[x_?type[Z2]]:=x evaluates f to 0, not to f, doing so without using extra brackets as used in the function g. Mar 1 '20 at 13:52
• This is an attemp of solution: type[x_,y_]:=MemberQ[x,y] and then f[x/;type[Z2,x]]:=x. Mar 2 '20 at 13:52
• Thanks for the suggestion! Your way gives the correct outcome as well, but requires explicitly specifying the argument of the function, so is not quite what I was hoping for. (Put differently, your workaround involves at least as much typing as adding the extra brackets that were needed in my definition.) In any case, thanks again for looking at this! Mar 5 '20 at 16:22
• fwiw, you can do t=type[Z2]; f[x_?t] := x
– kglr
Apr 9 '20 at 10:39