       MatchQ, RepeatedNull

• To: mathgroup at yoda.physics.unc.edu
• Subject: MatchQ, RepeatedNull
• From: wsgbfs at win.tue.nl (Fred Simons)
• Date: Tue, 14 Sep 93 15:01:00 +0200

```One of the things we like very much in Mma are patterns. Easy to work
with but in a sense difficult to understand.
In Mma we can construct patterns using blanks, names, defaults etc. and then
test if some Mma-tree satifies the pattern using MatchQ.
Here are some examples:

In:= MatchQ[ a + b, Plus[ _.. ] ]

Out= True

In:= MatchQ[ x^3, x^_ ]

Out= True

In:= MatchQ[ x^2 + x^3, Plus[ (x^_).. ] ]

Out= False

This result is unexpected. We ask wether x^2+x^3 satisfies the pattern
of a sum of any powers of x and Mma produces False.
After some puzzling we found that this is due to the evaluation of the
right-hand side, resulting in Repeated[Power[x, Blank[]]], while it should be
Plus[ ... ] or Repeated[Plus[ ... ]] to return True.

We can have Mma doing what we want by changing the attributes of MatchQ:

In:= Unprotect[ MatchQ]

Out= {MatchQ}

In:= SetAttributes[ MatchQ, HoldRest ]

In:= MatchQ[ x^2 + x^3, Plus[ (x^_).. ] ]

Out= True

Is there a specific reason why MatchQ does NOT have attribute HoldRest?

When playing around with MatchQ we also found curious behaviour of
RepeatedNull, as shown in the following output.

In:= MatchQ[ a, (_).. ]

Out= True

In:= MatchQ[ a, (_)... ]

Out= False

Why does RepeatedNull not produce True if Repeated produces True?

Also when defining functions strange things may happen:

In:= h[_...] := True

In:= {h[a], h[a,a], h[a,b], h[]}

Out= {True, True, True, True}

In:= Clear[h]; h[a...] := True

In:= {h[a], h[a,a], h[a,b], h[]}

Out= {True, True, h[a, b], h[]}

The fourth item should have been True, we think.

Is this a bug in Mma 2.0 on a NeXT or is there something we do not
understand?

Frans Martens, Fred Simons
Eindhoven University of Technology

```

• Prev by Date: Mathematica for elementary and junior high students
• Next by Date: Plotting without asyptotes.
• Previous by thread: Mathematica for elementary and junior high students
• Next by thread: Plotting without asyptotes.