WOLFRAM COMMUNITY
Connect with users of Wolfram technologies to learn, solve problems and share ideas
Join
Sign In
Dashboard
Groups
People
Message Boards
Answer
(
Unmark
)
Mark as an Answer
WOLFRAM COMMUNITY
Dashboard
Groups
People
0
|
3054 Views
|
4 Replies
|
1 Total Likes
View groups...
Follow this post
Share
Share this post:
GROUPS:
Mathematica
Contradictory behaviour in pattern matching
Hans Milton
Hans Milton
Posted
11 years ago
Hello,
MatchQ do not seem to treat the inbuilt functions Power and Times in the same way:
In[1]:= MatchQ[Power[a, b], Power[_, _]]
Out[1]= True
In[2]:= MatchQ[Times[a, b], Times[_, _]]
Out[2]= False
An explanation, or what am I doing wrong?
Hans
POSTED BY:
Hans Milton
Reply
|
Flag
4 Replies
Sort By:
Replies
Likes
Recent
1
Daniel Lichtblau
Daniel Lichtblau, Wolfram Research
Posted
11 years ago
You need
HoldPattern
to prevent that patter from evaluating to
Power[_,2]
.
MatchQ[Times[a, b], HoldPattern[Times[_, _]]]
Out[19]= True
POSTED BY:
Daniel Lichtblau
Reply
|
Flag
0
Hans Milton
Hans Milton
Posted
11 years ago
Thanks,
But could you please elaborate on why HoldPattern is needed in case of Times, but not Power?
POSTED BY:
Hans Milton
Reply
|
Flag
0
Daniel Lichtblau
Daniel Lichtblau, Wolfram Research
Posted
11 years ago
As I stated, Times[_,_] evaluates to Power[_,2]. This happens before MatchQ ever sees its arguments. Power[_,_] on the other hand evalautes to itself. The different matching outcomes are explained by these facts.
POSTED BY:
Daniel Lichtblau
Reply
|
Flag
0
Hans Milton
Hans Milton
Posted
11 years ago
Yes, as can be seen:
In[4]:= Times[_, _]
Out[4]= _^2
Thanks for very fast response!
POSTED BY:
Hans Milton
Reply
|
Flag
Reply to this discussion
in reply to
Add Notebook
Community posts can be styled and formatted using the
Markdown syntax
.
Tag limit exceeded
Note: Only the first five people you tag will receive an email notification; the other tagged names will appear as links to their profiles.
Publish anyway
Cancel
Reply Preview
Attachments
Remove
Add a file to this post
Follow this discussion
or
Discard
Group Abstract
Be respectful. Review our
Community Guidelines
to understand your role and responsibilities.
Community Terms of Use
Feedback