2

I am a little confused about:

In what order is a Rule tried to apply to an expression? In a depth-first order or otherwise?

For example,

f[x + f[y]] /. f[u_] -> g[u]

outputs

g[x + f[y]]

which looks like the rule was first tried at the outermost level. And "the result was returned as soon as the rule had been applied".

However, if the actual pattern part [u_] was dropped,

f[x + f[y]] /. f -> g

the output changed to

g[x + g[y]]

which looks like the new rule was first tried at the innermost level. And tried repetitively to the "shallower" levels.

If we follow the behavior observed in the first example, the output should have been

g[x + f[y]]

So why are two rules tried in different orders?

Naitree
  • 1,235
  • 9
  • 20

0 Answers0