I have this weird behavior of patterns inside the Module construct. Note that we have

In: Module[{}, a_->x]
Out: a_ -> x

While if x is scoped, then some garbage also appears in a

In: Module[{x}, a_->x]
Out: a$_ -> x$16509

Even more, it doesn't help if x has a value,

In: Module[{x}, x=4; a_->x]
Out: a$_ -> 4

Is there any explanation for this behavior/can it be avoided in some straightforward way? Note that this does not happen in Block.

The reason this is problematic for me is because I receive a global expression with variable a, and I want it to appear in the right hand side of the pattern, i.e.

In: expr=a^2; Module[{x}, x=4; stuff[a_]->expr * x]
Out: stuff[a$_] -> 4 a^2

P.S. Mathematica


marked as duplicate by Kuba Nov 29 '17 at 6:17

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.

  • 1
    $\begingroup$ Linked topic should answer you doubts and provide broader context. Let me know if you disagree with closing. $\endgroup$ – Kuba Nov 29 '17 at 6:18
  • $\begingroup$ @Kuba, thanks! I didn't realize this is such a general thing.. $\endgroup$ – Peter Kravchuk Nov 29 '17 at 6:23