Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

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

This question already has an answer here:

I am frustrated by the following:

{1, 2, 3.0} /. a_Integer -> a + 1

The a on the left side of the arrow is localized and appears in green whereas its sibling on the right side is blue. Consequently, I have to write:

Block[{a}, {1, 2, 3.0} /. a_Integer -> a + 1]

to avoid naming conflicts.

It doesn't make sense to me that, within one inbuilt function, a variable is treated differently. I already got some hard to find errors because I forgot to block.

What did I overlook here?

share|improve this question

marked as duplicate by Mr.Wizard Jul 15 '15 at 6:48

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

up vote 4 down vote accepted

In this case you need to use :> instead of ->:

{1, 2, 3.0} /. a_Integer :> a + 1

This will ensure effective localization.


share|improve this answer
Of course. I put my question as answered. Thank you very much. – eldo May 28 '14 at 17:07
@eldo Even though it is about the basics, this is a good question because it is very clear and to the point. I changed the title a bit to make it even more clear, in hope of making it useful for future visitors. – Szabolcs May 28 '14 at 17:08
Georg Cantor once remarked "In mathematics the art of proposing a question must be held of higher value than solving it." Looking at your simple :> I would say: "In mathematics the art of proposing an answer must be held of higher value than the underlying question." – eldo May 28 '14 at 21:29

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