Select[{{"foo", "bar"} -> "a", {"foo", "baz"} ->
"a", {"foo", _} -> "a"}, MemberQ[{{"foo", "bar"}}, #[[1]]] &]
(* {{"foo", "bar"} -> "a", {"foo", _} -> "a"} *)
I'd like the answer to be {{"foo", "bar"} -> "a"}. The built-in MemberQ seems to use MatchQ for testing equality. I'd like a memberQ that uses SameQ or TrueQ[Equal[#1, #2]]&, and works for possibly funky arguments such as patterns.
Verbatimthe best? What aboutHoldPattern,Literaletc.? – user13253 Jan 11 '13 at 06:46HoldPatternis related but different, and "Since Version 3.0 (released in 1996), Literal has been superseded by HoldPattern." – Mr.Wizard Jan 11 '13 at 06:47Cases[list, Verbatim[Rule]["foo", _]]andCases[list, HoldPattern["foo" -> _]]? For whatlistthe two can be different? – user13253 Jan 11 '13 at 22:51"foo" -> _can evaluate. However, consider this example:list = {"foo" -> 1, "foo" -> _, "bar" -> 2}; x = "foo";then:Cases[list, Verbatim[Rule][x, _]]versusCases[list, HoldPattern[x -> _]]versusCases[list, Verbatim[x -> _]]-- observe thatHoldPatternpreventsxfrom evaluating to"foo"and therefore nothing matches.Verbatim[Rule][x, _]prevents the rule from being seen as a replacement byCasesbut does not match_literally.Verbatim[x -> _]matches"foo" -> _literally. – Mr.Wizard Jan 12 '13 at 03:45