0
|
6676 Views
|
6 Replies
|
1 Total Likes
View groups...
Share
GROUPS:

# A Question about "Optional value in pattern test"

Posted 11 years ago
 I have a question about the pattern. The first one makes sense. I would like to know why the third (In[37]) one is not working.  In[33]:= MemberQ[{{}}, Optional[a_, 1]]  Out[33]= True  In[38]:= MemberQ[{1}, Optional[a : 1, 2]]  Out[38]= True  In[37]:= MemberQ[{2}, Optional[a : 1, 2]]Out[37]= FalseAny idea is appreciated.
6 Replies
Sort By:
Posted 11 years ago
 @Rui, Aha! I think your code explained the answer well. The interpretation difference should be the cause in my example. Many thanks to you and @Marteen.
Posted 11 years ago
 I agree with Maarten about what the real question is. It would be easier to answer if one didn't have to guess what leads you to think it should return something different.Do the results while replacing the Optionals by their first arguments confuse you? What about also removing the labels, and using MatchQ?MatchQ[1, _]MatchQ[1, 1]MatchQ[2, 1]TrueTrueFalseHowever, Optional's second argument not mattering for pattern matching is it's not as general as what Marteen said. TakeMatchQ[{}, {_}]MatchQ[{}, {_:9}]FalseTrue
Posted 11 years ago
 @ Rui.You are right in the sense that the presence of a second argument does indeed matter. I.e. using Optional[_,something] will allow matching of a Length zero element. However, in terms of matching it doesn't really matter what the second argument is.
Posted 11 years ago
 The real question here is why you think the third one should yield "True".As far as I understand the second argument of Optional should do nothing when you are just Matching patterns. (It only becomes relevant if you want to do something with the result of the matching, as it assigns a value to that part of the pattern even when missing.
Posted 11 years ago
 @Rui and @Maarten, Thanks for your reply. I was originally testing what type of interpretation for the ":" will be in the second and third input, pattern or optional/default value. If it is used as a Pattern, the whole pattern expression seems to be one thing and can be replaced. If the ":" still indicates a  optional value, then the In[37] should be something like Optional[1,2] while one is tested and it passed test (Out[38]). I was wondering if the 2 can be the optional value of 1. Usually it should not happen since you will get an error message by doing this: MatchQ[2, Optional[1, 2]]I was confused by my results. (@Rui, your results are clear and the outputs are the same as they should be) Apololgize for missing the scenario here.
Posted 11 years ago
 @Marteen, agree, I had misunderstood what you meant. @Shenghui, glad we helped. Sometimes while trying to explain a doubt clearly to others, one finds the answer. It happens to me all the time and I think it would have been your case here. If I understood correctly then the cause of the doubt was the peculiar parsing behaviour of the : operator, beign different if the left hand side is a symbol or a blank, right?a : b // FullForm // HoldForm_ : b // FullForm // HoldFormPattern[a,b]Optional[Blank[],b]