1
$\begingroup$

I failed to make a short pattern to match {1,b,a,4,g}.

my code is as shown below:

MatchQ[{1, a, 3, b, c, 5, 6}, 
 t_List /; MemberQ[t, _Integer | _Symbol]]

I guess there could be more clever way to make this pattern, such as using | and & .

$\endgroup$
0
1
$\begingroup$
MatchQ[{1, a, 3, b, c, 5, 6}, {(_Integer | _Symbol) ..}]
$\endgroup$
2
  • $\begingroup$ thank you soooo much, this is exactly what i want ! PS: Could you please recommend me some higher-level books about mathematica ? $\endgroup$ – Oliver Redman Nov 28 '16 at 11:58
  • $\begingroup$ @OliverRedman There isn't any book about mma I've read, I have learned mstly from here or sources/people linked to this forum. Here is something you can use: Where can I find examples of good Mathematica programming practice? $\endgroup$ – Kuba Nov 28 '16 at 12:12
0
$\begingroup$
data = {1, a, 3, b, c, 5, 6};

targets = {1, b, a, 4, g};

Intersection[data, targets]

(*  {1, a, b}  *)

Cases[data, Alternatives @@ targets]

(*  {1, a, b}  *)

Cases[targets, Alternatives @@ data]

(*  {1, b, a}  *)

Select[data, MemberQ[targets, #] &]

(*  {1, a, b}  *)

Select[targets, MemberQ[data, #] &]

(*  {1, b, a}  *)

Cases[data, _?(MemberQ[targets, #] &)]

(*  {1, a, b}  *)

Cases[targets, _?(MemberQ[data, #] &)]

(*  {1, b, a}  *)

DeleteCases[data, _?(FreeQ[targets, #] &)]

(*  {1, a, b}  *)

DeleteCases[targets, _?(FreeQ[data, #] &)]

(*  {1, b, a}  *)
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.