To avoid any conflict with the built-in symbol for \[Element]
, I would use the small element symbol, ∊
instead (this is a different unicode character from ∈
). Here is a way to define its use as an infix operator by way of a template:
ClearAll[myMemberQ]
appearanceIn[x_, y_] :=
TemplateBox[{x, y}, "myMemberQ",
DisplayFunction :> (RowBox[{#1, "∊", #2}] &),
InterpretationFunction :> (RowBox[{"myMemberQ", "[",
RowBox[{#2, ",", #1}], "]"}] &)]
myMemberQ[l_List, e_] := MemberQ[l, e]
myMemberQ /: MakeBoxes[myMemberQ[l_, e_], StandardForm] :=
appearanceIn[ToBoxes[e], ToBoxes[l]]
SetOptions[EvaluationNotebook[],
InputAliases ->
DeleteDuplicates@
Join[{"in" -> appearanceIn["\[Placeholder]", "\[Placeholder]"]},
InputAliases /.
Quiet[Options[EvaluationNotebook[], InputAliases]] /.
InputAliases -> {}]]
This defines a function myMemberQ
that returns the same as MemberQ
if the first argument is a list. If not, it defaults to a display that shows the arguments with the infix symbol ∊
between them. This display is defined in appearanceIn
. The same appearance function is also used to make the input appear in the form x ∊ Y
. To achieve that, I add an input alias that you invoke by entering escin
esc. Upon typing this, the template for the infix notation automatically appears.
With this, you can enter things like this:
2 ∊ {1, 2, 3}
(* ==> True *)
statement = x ∊ set
(* ==> x ∊ set *)
Above, the symbolic expression on the last line remains unevaluated because set
isn't a List
.
But you can work with the unevaluated expression as usual:
statement /. set -> {x, y, z}
(* ==> True *)
Likewise, the example in the question works as follows:
test = {1, 2, 3, 4}
find = {3, 4};
Select[test, (# ∊ find) &]
(* ==> {1, 2, 3, 4} *)
(* ==> {3, 4} *)
Note: Wherever the ∊
symbol appears in the code examples above, I entered it by typing escin
esc. So you can't copy and paste the code cells shown here. You have to use the input alias.
However, you can copy and paste forms like x ∊ set
within the notebook, and they will retain their functionality (i.e., they can later be evaluated).