3
$\begingroup$

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?

$\endgroup$
0
4
$\begingroup$

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

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

This will ensure effective localization.

Refereces:

$\endgroup$
3
  • $\begingroup$ Of course. I put my question as answered. Thank you very much. $\endgroup$ – eldo May 28 '14 at 17:07
  • $\begingroup$ @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. $\endgroup$ – Szabolcs May 28 '14 at 17:08
  • $\begingroup$ 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." $\endgroup$ – eldo May 28 '14 at 21:29

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