0

Consider the simple code

r`outsideFormula = x;
Module[{f, g, insideFormula = 2 x}, 
 f[x_] = r`outsideFormula;
 Print@f@1(*works*);
 g[x_] = insideFormula;
 Print@g@1(*won't work*)
 ]
(*
1
2x
*)

When the formula defined outside the Module is used in the RHS of Set, it works upon successive evaluations as expected. However if a Module variable is used (insideFormula), it apparently remains unevaluated.

Why is this?

lineage
  • 1,144
  • 4
  • 10
  • 2
    Print@DownValues@f to see what is going on and read avoiding-renamings to learn why – Kuba Jun 20 '22 at 10:48
  • 1
    @Nasser it is 2x but for f[ x$_], notice $. – Kuba Jun 20 '22 at 11:03
  • @Nasser Whether or not x is localized the Module in the second definition will rename x_ to x$_ making it different than x from the RHS. That is explained in the topic linked in my first comment. – Kuba Jun 20 '22 at 11:20
  • @Nasser didn't nest any modules here...Kuba's "renaming of x to x$" explanation seems correct – lineage Jun 20 '22 at 11:26
  • @Kuba the linked explanation is a bit difficult for me to understand (and long). If time permits, would you mind adding an explanation in simpler words? Originally, the problem was that some complicatedly named var Global'complicatedName had a formula with symbols. I opened a Module to evaluate it for different values of those symbols (instead of using /.symbol->value) and some other manipulations ...while doing all this, instead of rewriting everytime complicatedName, I thought I'll alias it in Module vars and that's when I found the difference. btw, your x$ explanation works – lineage Jun 20 '22 at 11:32
  • I doubt I can do a better job than what's in the first paragraph of the accepted answer there. I am encouraging you to try to go through it. – Kuba Jun 20 '22 at 14:39

0 Answers0