Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

1 Answer 1

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.

Refereces:

share|improve this answer
    
Of course. I put my question as answered. Thank you very much. –  eldo May 28 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 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 at 21:29

Your Answer

 
discard

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.