Community RSS Feed
http://community.wolfram.com
RSS Feed for Wolfram Community showing any discussions in tag Raspberry Pi sorted by activeUsing an Association to contain different equations within a Module
http://community.wolfram.com/groups/-/m/t/1384899
After much head scratching and experimenting, I've solved a particular problem - but I can't see why my first attempts did not work. So there's clearly something I'm not understanding here. Simplified code below:
myFuncA[xAssoc_] := Module[{},
tmpFuncA[x_] = xAssoc[a];
tmpFuncA[10]
]
myFuncB[xAssoc_] := Module[{},
tmpFuncStr = xAssoc[a];
tmpFuncB[x_] = tmpFuncStr;
tmpFuncB[10]
]
myFuncC[xAssoc_] := Module[{tmpFuncStr},
tmpFuncStr = xAssoc[a];
tmpFuncC[x_] = tmpFuncStr;
tmpFuncC[10]
]
myAssoc = <|a -> 1 + x, b -> 2 + x|>;
func[x_] = myAssoc[a];
func[10]
myFuncA[myAssoc]
myFuncB[myAssoc]
myFuncC[myAssoc]
I have an association with 2 simple equations in x. I can assign one of these 'a' to a function and then pass a value to it - func[10]. The output is as expected - 11.
However, I wanted this to be used inside another function, hence myFuncA. This contains essentially the same code,however the output is now 1+x
To get the correct result,I had to use an intermediate variable as in myFuncB which gives the correct - 11 - output.
But being a mindful programmer, I like to keep the scope of temporary variables as limited as possible. So I wrote myFuncC. The output from this 1+x
myFuncB works but myFuncA does not.
And myFuncC breaks it again.
What am I missing here? There must be something about variable scope I am not getting. Can anyone enlighten me perhaps.
[Edit: Mathematica 11.2 on Raspberry Pi]Paul Scanlon2018-07-16T18:39:07Z