Algebra is so much fun but I'm struggling to understand how MMA executes local variables within Module
. Reading the Module
documentation page I suspect it has something to do with "Before evaluating expr, Module substitutes new symbols for each of the local variables that appear anywhere in expr except as local variables in scoping constructs". But as I work through possible examples of how this works, the output behaviour continues to unpredictable. For example, what is causing the difference in output below? How would you explain this to a kindergartener? Or one's great grandmother?
In1= x=2;y=x+2;y+x
6
in2= Module[{a=2,b=a+2},a+b]
4+a
Module[{a = 2, b}, b = a + 2; a + b]
; the way you wrote it will not haveb
inherit the value ofa
. $\endgroup$