Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Suppose I was trying to find out if the sequence 34 appeared in the digits of 344444. I tried to do this with MemberQ and get:

MemberQ[RealDigits[344444][[1]], {x___, 3, 4, y___}]


But if I add parentheses around the argument I get:

MemberQ[{RealDigits[344444][[1]]}, {x___, 3, 4, y___}]


RealDigits[344444][[1]] already gives me a list, why do I need to nest it inside another one? I thought it could be due to whether or not MemberQ checks the head of the list, but putting Heads -> True or Heads -> False does not alter the result.

share|improve this question
I believe that MatchQ[RealDigits[344444][[1]], {x___, 3, 4, y___}] is what you want. –  David Carraher Jun 5 '13 at 1:57
Thanks for that, I see how that works. –  Andrew Ledesma Jun 5 '13 at 2:12
add comment

closed as too localized by Oleksandr R., Sjoerd C. de Vries, Rojo, Artes, Silvia Jun 5 '13 at 12:32

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

up vote 7 down vote accepted

The answer is simple, you used the wrong function. If you want to find out whether some expression matches a pattern, you have to use MatchQ

MatchQ[RealDigits[344444][[1]], {x___, 3, 4, y___}]

MemberQ is used when you want to test whether any member of a list matches some pattern. If you put additional braces around your expression, then you make a list with one member you want to test. That's why it returns true with additional braces.

share|improve this answer
Thanks for that, that makes perfect sense. –  Andrew Ledesma Jun 5 '13 at 2:06
add comment

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