5

For example, I have the equation

x^2 + y^2 == x*y

and I want to apply the rule

y -> s*x

I can do it easily by

x^2 + y^2 == x*y //. y -> s*x 

But it seems to me it would be possible to use only Map to change everything. How can I do it that way? In other words, I think Replace is just a kind of syntactic sugar, and want to know how to express Replace by Map.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
poorGuy
  • 51
  • 1

1 Answers1

5

As expressed in the comments, the Replace functions are not merely "syntactic sugar" for Map. The two are quite different. One primary difference is the order in which expressions are visited. See:
How to perform a depth-first preorder traversal of an expression? Another is that Replace will go inside held expressions, while Map does not evaluate:

Hold[1 + 2 + 3] /. {2 -> 7}
Hold[1 + 7 + 3]
Map[Print, Hold[1 + 2 + 3], {2}]
Hold[Print[1] + Print[2] + Print[3]]

With the exception of these important differences, one can do something resembling a ReplaceAll using MapAll, like this:

SetAttributes[f, HoldAll]

f[y] := s*x
f[other_] := other

f //@ (x^2+y^2==x*y)
x^2 + s^2 x^2 == s x^2
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371