Message Boards Message Boards

GROUPS:

Pattern Matching with And[a,b]

Posted 3 months ago
464 Views
|
2 Replies
|
3 Total Likes
|

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

2 Replies

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

Posted 3 months ago

Thank You, that is working fine:

In[913]:= Replace[And[a, b], {HoldPattern[And[x__]] :> List[x]}]
    Out[913]= {a, b}
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