Consider the following code:
list={1,2}
MemberQ[list, x_ /; (x == #)] & /@ list
This gives {True,True}
MemberQ[list,form]
means that an element of List matches form
,
But how does mathematica know what to put for the pattern
x_
?
Asked differently, I view the above MemberQ
as doing the following:
- MemberQ is a pure function as input above
- Map (/@) applies list to the MemberQ (pure) function
- This means that Map evaluates MemberQ with each element of
list
in place of#
- This means that Map evaluates MemberQ with each element of
- What part of the code tells mathematica what to use for the named pattern
x_
??. (in other words, how does mathematica know what $x$ is when checkingx==#
is true or not)
I thought maybe Map
was applying the list elements to both #
and x_
, but
f[x_, #] & /@ {5}
gives {f[x_,5]}
Edit: An example of where this construct is potentially relevant: This answer
Edit 2: I think maybe this is what is happen:
First let us look at MemberQ[list,x_]
. This will give True
For (almost?) list. What is this doing? This is saying does list
match the form of Pattern[x,Blank[]]
. Well, Pattern[x,Blank[]]
, is just any expression, so yes list matches.
Now let us consider MemberQ[list,x_/;(x==#)]
. This is asking whether list matches patterns that are an expression AND have (x==#)
. Well, if #
is an expression, it will match (the condition will be satisfied), and since we are doing /@list
, we are asking if expressions of the form in list
match elements in list
, which is true.
Trace
around the computation might give some information about the binding. $\endgroup$