Message Boards Message Boards

0
|
3720 Views
|
2 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Pattern Matching with And[a,b]

Posted 4 years ago

Could someone help me to explain the following strange behavior of Mathematica with the following expression:

In[903]:= Replace[And[a, b], {And[x__] :> List[x]}]
    Out[903]= {a && b}

in comparison to:

In[891]:= Replace[and[a, b], {and[x__] :> List[x]}]
    Out[891]= {a, b}

I expected to get the result of the second example {a, b} for the first expression instead of {a && b}. In the Pattern "And[x__] " the "And" seems not to be properly recognized for substitution, so that it is still remaining in the result {a && b}.

The second example with the meaningless symbol "and" is working as expected. I guess it is a special behavior of the BuildIn-Function "And".

Thank you in advance. Uwe Möhring

POSTED BY: uwe moehring
2 Replies
Posted 4 years ago

Thank You, that is working fine:

In[913]:= Replace[And[a, b], {HoldPattern[And[x__]] :> List[x]}]
    Out[913]= {a, b}
POSTED BY: uwe moehring

Because And[x___] is evaluated to x___ before pattern matching. You need HoldPattern here.

POSTED BY: Shenghui Yang
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract