3

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?

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
eldo
  • 67,911
  • 5
  • 60
  • 168

1 Answers1

4

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

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

This will ensure effective localization.

Refereces:

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
  • 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