18
$\begingroup$

Bug fixed in 10.1


Consider the following pattern-matching expressions:

MatchQ[{1, 2}, {a_, b_}]                     (* True *)
MatchQ[{1, 2}, {a_, b_:0}]                   (* True *)
MatchQ[{1, 2}, {PatternSequence[a_, b_]}]    (* True *)
MatchQ[{1, 2}, {PatternSequence[a_, b_:0]}]  (* False *)

The last result surprises me. I would have expected it to return True just like the others. I suspect a bug, but does this behaviour conform to some pattern-matching principle that I have overlooked?

$\endgroup$
5
  • 1
    $\begingroup$ I reported this issue to WRI some years ago, but never received a diagnosis. $\endgroup$
    – WReach
    Jan 22, 2015 at 15:16
  • 6
    $\begingroup$ Interesting. Yes, probably a bug. I'll try to have a look when I get a chance. $\endgroup$ Jan 22, 2015 at 15:57
  • $\begingroup$ To my interest MatchQ[{1, 2}, {PatternSequence[a_, b_.]}] throws messages but returns True. $\endgroup$
    – Mr.Wizard
    Jan 23, 2015 at 23:01
  • $\begingroup$ @Mr.Wizard Working on it (and the issue from the original post). $\endgroup$ Jan 23, 2015 at 23:15
  • 1
    $\begingroup$ @Daniel i.stack.imgur.com/k7L5x.gif $\endgroup$
    – Mr.Wizard
    Jan 23, 2015 at 23:18

1 Answer 1

8
$\begingroup$

Fixed in 10.1 (windows)

Mathematica graphics


Mathematica graphics

code

MatchQ[{1, 2}, {a_, b_}]                     
MatchQ[{1, 2}, {a_, b_: 0}]                 
MatchQ[{1, 2}, {PatternSequence[a_, b_]}]    
MatchQ[{1, 2}, {PatternSequence[a_, b_: 0]}]  
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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