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$look at each part of the expresison
does it traverse down the expression tree or it has some other mechanism? $\endgroup$Replace
,Cases
, etc. ForReplaceAll
it is actually BFS. $\endgroup$ReplaceAll
is not a BFS but rather a depth-first preorder traversal? $\endgroup$