All Questions
Tagged with scoping programming
34 questions
2
votes
0
answers
63
views
Why do notebooks by default put symbols in Global context where they can collide with all other notebooks?
This default setting can cause subtle and difficult to find problems, such as inconsistent or incorrect answers when symbols, variables or functions in different notebooks have the same name. I speak ...
18
votes
2
answers
562
views
Garbage collection for lexical closures
Mathematica is known not to garbage collect temporary variables in certain cases. I am particularly interested in the very useful programming pattern known as a lexical closure. A somewhat trivial ...
0
votes
1
answer
58
views
Program works, but then fails when I write it inside Module [duplicate]
I'm new to Wolfram Language; I'm trying to get used to it by writing a bunch of code and programs, being one of them the algorithm for the bisection method.
When I write the algorithm outside of a ...
0
votes
0
answers
57
views
Block Behavior, but for Function Assignment instead of Variable Assignment
We can define a function using a variable b that does not appear as an argument, and then using Block, change the output of the ...
3
votes
0
answers
92
views
Module inside With, With inside Module, or Just Module?
In general, if one wants to define local variables and local constants, which of the following is the best programming practice in terms of performance and code "safety"? (Assume ...
2
votes
3
answers
93
views
Strange Behavior of Compile[] inside With[]
Consider, in the examples below, the difference between defining $n$ as a global variable or as an argument to function comp[], which includes a compiled function ...
1
vote
1
answer
155
views
How to use Module function?
Here is my issue; I want to iterate a fit to this data for a polynomial of degree 2 up till degree 6. However, at each iteration I drop certain data points that have a poor residual value. So each ...
1
vote
0
answers
67
views
ConditionalExpression still resulting from Assuming a Module's local variables
I have a memoizing, recursive function whose MWE looks like this (the two fake inputs/ICs are from 'K' and 'Bfun2', the latter just a quick way to check the results coming out of the Residue.:
...
3
votes
3
answers
240
views
What is wrong with Module? or me? [closed]
The following code does not give me an answer. It looks all is working when each command inside Module is individually run. But ...
0
votes
1
answer
131
views
Declaring global variable in module/ Looping in mathematica [closed]
I'm new and still learning about mathematica. I'm having some trouble with my code. This is my current code that i'm having trouble with:
...
1
vote
1
answer
293
views
call function to trigger update to dynamic -- what's the right way
I want to create a plot using Dynamic. Ideally, I'd have some persistent, locally scoped state variables living in a Module; as ...
7
votes
2
answers
178
views
How to create Functions that have Compiled functions effectively
I'm about to build a package that will provide lots of simple functions. I want these functions to run as fast as possible when evaluating numerically but still provide symbolic results when needed. ...
32
votes
1
answer
964
views
Is the renaming mechanism of `With` flawed?
I'm confused about the renaming mechanism of With:
...
6
votes
2
answers
1k
views
What does empty {} mean in Module? [duplicate]
I have a question. I would like to know what an empty list ({}) means when used in Module[] as the code below shows? Why has a ...
2
votes
1
answer
136
views
Code optimizations with functions inside Module
I wrote a small code by using functions inside Module:
...
5
votes
3
answers
700
views
Turning an expression into a function inside another function
Say I have an expression
g=Sin[q];
It is easy to turn it into a function, and to perform manipulations on this function (this and variants of this were discussed ...
1
vote
0
answers
191
views
Should I Remove the initial symbols of Module?
Often we use Module to construct complicate function. Consider that Module create new vars every time we run the function, should I remove the initial values at the end of Module-function?Or should I ...
4
votes
1
answer
528
views
ControlType -> None Vs. Module inside Manipulate (i.e. making everything local)
I'm building a program on another one made available in the Wolfram Demonstration Project.
I've noticed that in the original code (working flawlessly) many variables were made local inside the ...
6
votes
4
answers
651
views
Expressions containing globally undefined symbols inside a function where they are defined
Consider the following code.
f[a_,b_]:=x
x=a+b;
f[1,2]
(* a + b *)
From a certain viewpoint, one might expect it to return 3 ...
9
votes
2
answers
425
views
How to assemble and disassemble a function into steps
I've watched many programmers (both novice and expert) write functions in Mathematica and have observed a common behavioral pattern. This pattern has many elements that would be automated and thus ...
0
votes
0
answers
127
views
Best practices for breaking up functional blocks?
Say I have a main function that does some job, and I'm only interested in the final result of ans, i.e., the local functions are not important and produce no side-...
5
votes
3
answers
238
views
Prevent iterator name from being confused with symbol passed into function body
I have a massive amount of code with lots of Table and Sum inside a Module. Each with their ...
1
vote
0
answers
98
views
Settings to disable global scope
I am building a fairly large application in Mathematica, and globally scoped variables are causing issues.
I would like globally scoped variables to be out-of-scope for my expressions, except by ...
3
votes
1
answer
430
views
Call Functions From File Without Modifying Context (Sandbox)
Let's say I have file named test.m containing
test[arg1_] := ( Print[arg1];)
What is the best technique for calling test like ...
8
votes
1
answer
176
views
Is it possible to write a function immune to symbol redefinitions in any Block surrounding its invocation?
Normally, semantics of symbols used in a function definition can be changed by their redefinitions in Blocks surrounding the function invocation:
...
10
votes
1
answer
177
views
Is it possible to use the global definition of a symbol as part of its new local definition set in a Block?
Consider the following expression:
Block[{Print = CopyToClipboard}, doSomething[]]
Now all calls to Print from ...
3
votes
1
answer
149
views
Odd edge-case behavior of Block
In this example, Block is used to localize the variable cache as used in the function g when called from the function ...
57
votes
2
answers
2k
views
How safe is the use of Block and Internal`InheritedBlock
For many questions on this site answers are given which suggest to use Block and, especially for more advanced topics, ...
4
votes
3
answers
522
views
Modify this code using Module and While
I have written a recursive function and would like to re-write the code using Module AND While to compare the timings.
Here is my recursive function for f[n], where 6 n f[n] = f[n-1] + n! for n>0 and ...
17
votes
1
answer
541
views
Is the sharing of variables in Module/Block within Compile documented behavior?
Today I noticed something, I think for the first time.
When used inside Compile variable values within Module (and ...
10
votes
4
answers
1k
views
How to set Block local variables by code?
I need to create a user-defined Block function where the Block variables' values are defined by code. For example, imagine I ...
17
votes
1
answer
564
views
Modules that initialize themselves on first call
I use a lot of functions that extract a specific data item from a file with many data items. I want these functions to load data (slow) and return the item (fast) on first call, but just return the ...
5
votes
1
answer
636
views
How to avoid conflicts between local variable names and symbolic arguments in Block constructs?
I am using Block inside the definition of some recursive functions.
If the Block definitions use short variable names, like ...
9
votes
6
answers
956
views
How can you give a Module a context and have its local variables and Modules belong to that context?
What would be the best way to give a module its own context and have its local variables and Modules belong to that context?