Skip to main content
1 of 3
StarBucK
  • 2.2k
  • 1
  • 10
  • 35

Is there a difference between Replace with parameter "All" and ReplaceAll

Here is my extremly basic understanding of Replace and ReplaceAll.

This post is also a way for me to check if I understood the mechanism behind, if you see mistakes in my explanation don't hesitate to correct me !

Replace is a function that will apply replacement rules on part of expression.

However, it will apply the replacement rules at specific level given in parameters (by default it will be {0} corresponding to the whole tree).

So here :

Replace[x^2 + 1, x^2 -> a]

It doesn't do anything as x^2 is a subpart of the tree but it is not the whole tree in itself.

I could do :

Replace[x^2 + 1, x^2 -> a, All]

To make it work. Then the code will look at all the levels of the trees (thus all the subtrees) and look for a matching replacement.

I could also do :

ReplaceAll[x^2 + 1, x^2 -> a]

And here is my question : is there actually any difference between using

Replace[expr, rule, All]

and

ReplaceAll[expr,rule]

or it is indeed the same thing ?

StarBucK
  • 2.2k
  • 1
  • 10
  • 35