Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
"I think Replace is just a kind of syntactic sugar" - no. No, it's not. – J. M. May 16 '13 at 2:00
@J.M. Agree. But perhaps it could be interesting if the OP tells us why he thinks it is – Dr. belisarius May 16 '13 at 2:04
@J.M Oh then they are totally different? – poorGuy May 16 '13 at 2:07
I think it would be possible if there is a "function" that changes y to s*x. It seems rules are not the function...... – poorGuy May 16 '13 at 2:09
Yes, they act quite differently; Map[f, {1, 2}] will apply the function f on the elements of the list {1, 2} to yield {f[1], f[2]}. That wors differently from Replace[{1, 2}, 1 -> 5, {1}] which replaces the 1 in {1, 2} with 5, to yield {5, 2}. – J. M. May 16 '13 at 2:22

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
share|improve this answer
Nevertheless, the last use of //@ just feels like using a pipe wrench to whack nails... – J. M. May 16 '13 at 3:22
Oh god Thank you! – poorGuy May 16 '13 at 3:29

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.