This question has essentially been asked many times before, perhaps most recently here:
I addressed it myself when I demonstrated the use of Replace and FixedPoint in place of ReplaceRepeated to get the standard traversal in:
As already explained by RunnyKine ReplaceAll and ReplaceRepeated traverse the expression in an unusual way within the context of Mathematica. See my self-Q&A for some examples:
Further, as stated in the documentation:
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.
Combined with the traversal order this means that inner expressions will never be replaced if they are part of a larger expression that matches a rule.
mfvonh comments that you could use:
a[b[c, d]] //. (head : Except[newHead])[arg__] :> newHead[arg]
newHead[newHead[c, d]]
However this is inefficient as the entire expression is rescanned for each replacement. I explored this issue in some detail here:
seismatica wrote:
I think I now know (how) ReplaceAll scans an expression. I'm still confused about how it replaces its matches. For example h[h[u, h], h] /. x : head_[arg__] /; Print[x] :> x finds all heads h, yet h[h[u, h], h] /. h[arg__] :> k[arg] only replaces the outermost h. @mfvonh mentions in his comment that "ReplaceAll "quits" because it replaces the whole expr and doesn't test the new part". If that's the case, then why would h[h[u, h], h] /. h :> k even though when any h is replaced, the expressions would also change.
Each case you cite is different from the original. In the first example you use a Condition that never is True, therefore every part of the expression that matches the pattern (without the condition) is checked (and printed) as ReplaceAll searches in vain for a match. Note how the behavior changes if we use a condition that is always True:
h[h[u, h], h] /. x : head_[arg__] /; (Print[x]; True) :> x
h[h[u,h],h]
h[h[u, h], h]
The second example highlights an important yet perhaps subtle difference: only entire expressions that are replaced are skipped for further matching and replacement. An expression head is like any other part of an expression and it can be replaced independently, therefore you can use /. to replace all heads of a certain kind (or which match a certain pattern) but you must not include arguments along with them.
Consider these examples:
SeedRandom[0]
expr = RandomInteger[9, {3, 2, 3}]
{{{7, 0, 8}, {2, 1, 5}}, {{8, 0, 6}, {7, 2, 1}}, {{0, 6, 1}, {2, 8, 6}}}
expr /. {x__} :> h[x]
h[{{7, 0, 8}, {2, 1, 5}}, {{8, 0, 6}, {7, 2, 1}}, {{0, 6, 1}, {2, 8, 6}}]
expr /. List -> h
h[h[h[7, 0, 8], h[2, 1, 5]], h[h[8, 0, 6], h[7, 2, 1]], h[h[0, 6, 1], h[2, 8, 6]]]
This head replacement can be used, for example, to sort all lists:
expr /. List -> Composition[Sort, List]
{{{0, 1, 6}, {2, 6, 8}}, {{0, 6, 8}, {1, 2, 7}}, {{0, 7, 8}, {1, 2, 5}}}
Replacework but notReplaceAll? – seismatica Jul 28 '14 at 02:34//.would still not work is that each time the rule is repeated, it keeps on replacing thenewHeadagain withnewHead, without ever touching theb? – seismatica Jul 28 '14 at 03:04(head: Except[newHead])[arg__]to avoid infinite recursion. – mfvonh Jul 28 '14 at 03:08Except), and not mine (a[b[c, d]] //. head_[arg__] :> newHead[arg]) would touch the head? My use of//.did not work even though yours does (nice trick btw). – seismatica Jul 28 '14 at 03:36ReplaceAllapplying from the inside out? The closest thing I've found is this, from ref. – seismatica Jul 28 '14 at 04:31h[x + h[u]] /. h :> kwork to replace all headhtok, even though onehis clearly within anotherh. I'm again confused O.o? – seismatica Jul 28 '14 at 04:35ReplaceAllwork withh[x_]the same way that it works with stuff like simplyh(orTimesin the book)? In other words, why wouldReplaceAllreplace simple stuff in the latter (seemingly at all levels) but quit after the first replacement ifh[x]is involved? – seismatica Jul 28 '14 at 05:07Replace[...] acts from the inside out" rather missleading. What aboutReplace[a[b[c, d]], head_[arg__] :> newHead[arg]]? – sebhofer Jul 28 '14 at 09:20a[b[c[d]]] /. x_ /; Print[x] -> xandReplace[a[b[c[d]]], x_ /; Print[x] -> x, {0, Infinity}]. Notice the order, and how the second doesn't test heads likeaandbalone. This is becauseReplacetries to transform the "entire expression", and with the levelspec we have told it to do this at each level. So it starts at the inner parts of the expr and keeps widening its selection as it looks for a match.ReplaceAllis only replacing parts, so it can just walk forward through the tree. It "quits" because it replaces the whole expr and doesn't test the new part – mfvonh Jul 28 '14 at 12:16