I have been trying to understand how mathematica does pattern matching:
{Sin[x] + 1} /. Sin -> Cos
returns
Cos[x] + 1
I am guessing that the program searches for the expression in the expression tree and then replaces the pattern once it finds it.
Thanks
ReplaceAll
: ReplaceAll looks at each part of expr, tries all the rules on it, and then goes on to the next part of expr. The first rule that applies to a particular part is used; no further rules are tried on that part or on any of its subparts. $\endgroup$ – vapor Mar 21 '17 at 17:17look at each part of the expresison
does it traverse down the expression tree or it has some other mechanism? $\endgroup$ – Arihant Parsoya Mar 21 '17 at 17:32Replace
,Cases
, etc. ForReplaceAll
it is actually BFS. $\endgroup$ – Leonid Shifrin Mar 21 '17 at 17:52ReplaceAll
is not a BFS but rather a depth-first preorder traversal? $\endgroup$ – Mr.Wizard Mar 21 '17 at 18:28