Tag Info

New answers tagged

5

This bug is fixed in V10.0.1.0 but the behavior is different based on evaluation sequence. If both expressions are in a single cell you get the expected empty list: Do[Module[{foo}, foo[x_] := 1; foo[0]], {100}]; Names["foo$*"] {} If they are evaluated one at a time in different cells you get a single foo$ symbol regardless of how many times the Do ...


2

This will probably be closed as a duplicate of either: What are the use cases for different scoping constructs? Passing function as argument In the meantime the simple answer is that you need Module rather than Block, because the former creates a new Symbol whereas the latter merely temporarily changes the value of Symbol. myf = Module[{f}, f[3] = 33; ...


2

When Mathematica parses the definition LocalTestModule[]=Module[{localX, localY}, localX=1; localY=2;] the parser finds the following symbol names: LocalTestModule, Module, localX, localY. While Module is found in the System` context, LocalTestModule, localX and localY are not found, and are therefore created in the current context (which in ...


1

I've simplified your example a bit (the package doesn't seem to be relevant): Module[{localX, localY}, localX = 1; localY = 2;] My expectation is that I should not be able to set the value of the localX variable from the notebook because it is local to LocalTestModule. If it is truly local to LocalTestModule[], it should not be visible outside of ...


0

Are you certain that a and b in the first example are Global? In version 10 they do not appear to be: (The sliders of one Manipulate do not affect those of the other.) A look at the Cell Expression for one of these shows: . . . DynamicModuleBox[{$CellContext`a$$ = -3.3999999999999995`, $CellContext`b$$ = 0.21999999999999997`, . . .] . . . It would ...


4

flatten and flattenblock themselves provide an example of how Module and Block differ in behaviour: flatten[{a, {result}}] (* {a, result} *) flattenblock[{a, {result}}] (* {a} *) It is easy to imagine flattenblock being called from within another function that uses the symbol result as a variable name. The results would likely be unexpected. It is ...


3

Table and Sum scope their variables in the manner of Block. Effectively your code is like this: f[x_] := Block[{i = 5}, x] f /@ {a, i} {a, 5} This is actually a very useful aspect of Table but in this case it is also the source of your problems. Since you must localize the iterators one of the best solutions is the one you reject out of hand which ...


2

If I understand correctly, your problem is that you do not want to have to comb through this block of code manually to find which variables are being used as iterators. Fortunately, you can do this with pattern matching. Say we have this definition: ClearAll@"Global`*"; function[x_] := Module[{}, notanswer = Sum[j^2, {j, 10}]; answer = Table[x, {i, ...


5

Make use of Module's capability to localize variables. f[x_] := Module[{i}, Table[x, {i, 1, 3}]] f[i] {i, i, i} Also, with i localized, you don't need to use distinct iterator names in different iteration constructs. g[x_] := Module[{i, a, b}, a = Table[x, {i, 3}]; b = Table[x^3, {i, 2}]; {a, b}] {g[i], g[a], g[b]} {{{i, i, i}, {i^3, ...


3

It is not clear to me what you desire. You wrote: I know I can do this using Block wrapped around the call to f and g Yet you did not articulate why this is not the solution that you want. Rather than wrapping the call to (use of) f and g I would put Block inside those functions: f[x_] := Block[{h = Plus}, h[5, x]] g[x_] := Block[{h = Power}, h[5, ...


5

You are correct here: it would appear to be appropriate only when the precise pattern of h within f or g is known. The reason for this is that the pattern engine generally moves outside-in as it is rewriting expressions. A simple example is this: a[b[c[d]]] /. s_Symbol :> (Sow@s; s) // Reap {a[b[c[d]]], {{a, b, c, d}}} By contrast, actual ...


1

I am not sure I completely understand, but perhaps something like (using TagSetDelayed): h /: f[h[s_]] := f[s^2 + a] h /: g[h[s_]] := g[s^3 + b] or perhaps more simply (but I personally find harder to "read") f[h[s_]] ^:= f[s^2 + a] g[h[s_]] ^:= f[s^3 + b] now h has been defined as follows: f[h[x]] (* f[a + x^2] *) and g[h[x]] (* g[b + x^3] *) ...



Top 50 recent answers are included