3:34 AM
@xslittlegrass Sounds like a fancy way to say Flash Gordon!

3:51 AM
@kirma Exactly, we are working in femtosecond and attosecond regime (10^-18 second) :)

:)

4 hours later…
7:36 AM
Can someone remind me a thread about Block vs Module and stuff like this:
```Module[{ sol (*without sol it works even though it shouldn't matter, intuitively*)},
sol = First@DSolve[y'[x] + y[x] == a Sin[x], y[x], x];
func[x_] = y[x] /. sol;
DownValues@func
]```

@Kuba Weird.

@Szabolcs yes indeed, yet I feel I read about something at least closely related.
@Szabolcs even smaller example:
```Module[{test},
Defer[f[x_] = y[x] /. test]
]```

@Kuba Ah, yes! It's the `x`. I'm stupid.
I kept looking at `sol`.
That's to prevent scoping conflicts between `Set` and `Module`.
I don't fully understand this mechanism.

@Szabolcs I don't think you are, it isn't obvious :)

@Kuba This is related:
3

What you are trying to do is generate code (a function definition) programmatically. There are several techniques to programmatically generate code. The main tool we need is injecting held expressions into other held expression. The usual way to do it is using With: tmp2 = x^2 + 1; With[{exp...

But it doesn't explain it.
Without such a mechanism this wouldn't work:
```With[{x = 5},
Print[x];
Print[Function[x, x][1]];
]```

7:53 AM
@Szabolcs yep, and that isn't the topic I had in mind.

But `Function` does indeed have it's own local `x` here, independent from `With`. Achieved by adding a `\$` at the end.

Not the best example since `x` is scoped in `Module` and `Function`
while my example doesn't scope `x` in `Module`.

under details, "With constructs can be nested in any way, with inner variables being renamed if necessary. "

I feel like there was something from @Wreach explaining further that ref page. It's a good one, thanks.

Only cursory mention here, not quite the thing: mathematica.stackexchange.com/a/569/12
There's also `SetSystemOptions["StrictLexicalScoping" -> True]`, which changes how these things work, but doesn't fix your example.
It does cause the version without `sol` to gain a `\$` after `x`, without breaking the `func` definition. Again, I don't understand how lexical scoping works in Mma ...
I understand what Block and Module do in general, but not what happens when a single `\$` gets added.
@Kuba Go to `Set` docs, see the mention under "Details" (reference.wolfram.com/language/ref/Set.html) and then click the >> to go to the relevant example.

7:59 AM
@Szabolcs it seems that outer scoping constructs do that to all inner scoping constructs, just in case

Yes, that's right.
With, Module, Function, Set, Rule, etc. are all scoping constructs like this
"When it appears in an unevaluated symbolic form, Set is treated as a scoping construct so that variables in nested occurrences are renamed if necessary. »"
"The pattern variable is renamed if necessary inside a nested scope:"
BTW the fix is to use `DSolve[y'[x] + y[x] == a Sin[x], y, x]`.
Return `y` instead of `y[x]`.

@Szabolcs yep, that's it. Not so obvious unless you are paying attention

Many examples of weird looking behaviour come up with this search: mathematica.stackexchange.com/search?q=StrictLexicalScoping

@Szabolcs maybe, but I don't like this because you have to tell a user, if you want your code properly scoped you can solve for `y[x]`...
@Szabolcs thanks a lot for help.

I guess the most educational explanation is to just say that `=` is a scoping construct, so the `x` in the pattern is distinct from the `x` inside of `sol`. Without discussing renaming too much.
It seems much more natural to solve for `y` anyway, since we are looking for a function and shouldn't care much about what variable is in the argument

8:09 AM
@Szabolcs I can't agree about not discussing renaming it is one of basic mechanisms in WL yet I feel it is not stressed enough (or I'm just complaining as often) :)

8:22 AM
@Szabolcs I think this deserves a separate thread, compare:
`Module[{test}, Defer[f[z_] = y[z] /. test]]`
> f[z\$_] = y[z\$] /. test\$7080
and
`Module[{test}, Defer[f[z_, test_] = y[z] /. test]]`
> f[z_, test_] = y[z] /. test

@Kuba What I meant was that for someone not very experienced with Mma, the best way to think about it first is in terms of scoping. How that scoping is implemented (and how it gets extremely messy at that point) is the next step.
I would really like to see an answer explaining how lexical scoping works in detail, and what `"StrictLexicalScoping" -> True` changes exactly. With examples.

@Szabolcs ask the question then :P

@Kuba It's a lot of work to ask the question well ... one has to come up with a sufficient number of sufficiently mysterious examples, to make sure the answer will be good.
Can you help me with that?
For a start, why is there no renaming here, yet scoping still works? `Function[x, Function[x, x]]`
Does the renaming happen during evaluation time? `Function[x, Function[x, x]][1]` returns `Function[x, x]`.

@Szabolcs before I try, related and important input: How Many Scope Strategies Are There?

I have not seen this before.

8:41 AM
> The named formal parameters Subscript[x, i] in Function[{Subscript[x, 1],[Ellipsis]},body] are treated as local, and are renamed Subscript[x, i]\$ when necessary to avoid confusion with actual arguments supplied to the function
so I suppose scoping is done at evaluation (when function is used) stage

@Kuba I think I understand the scoping mechanism relatively well. It is different for lexical and dynamic scoping. In both cases, there is a variable conflict resolution and variable binding stage, during which, for lexical scoping, the outer construct is free to rename variables in inner constructs. For dynamic scoping, renaming is not needed. Here are some assorted links to related posts on SE and SO
35

Using global variables the following turns an "expression" into a Function: expr = 2 x; Function[x, Evaluate[expr]] Of course this doesn't work if for some reason a value is assigned to x. Thus, I want to turn expr into a Function iside a Module. But it seems like there is a scoping problem: ...

20

I want to describe an issue I have been having with Plot using With to keep defined parameters 'local'. I am not necessarily asking for a fix: the problem I have is one of understanding. Sometimes I use a construction such as the following to obtain a Plot: Method 1 plot1 = With[{vmax = 10, km...

12

I have the following pair of things: ClearAll[foo, labeledFoo]; labeledFoo = {"FooBarBazQuux", foo}; This works like you'd expect: labeledFoo /. hdr_String :> StringReplace[hdr, l_?LowerCaseQ ~~ U_?UpperCaseQ :> l <> " " <> U] (* {"Foo Bar Baz Quux", foo} *) So does this: labeledFo...

20

I feel like I must be missing something simple and obvious here, but this has me scratching my head. This works as expected: list = {f[a], f[b]}; Cases[list, f[x_] :> x] -> Position[list, f[_]] (* {a, b} -> {{1}, {2}} *) However, this does not: fun[list_] := Cases[list, f[x_] :> x] -> Posit...

10

I did a handful of tests with Block, With, and Module and had a couple of new surprises. First, I explicitly nested all pairs, producing the following nine tests, naively expecting 37 in each case: {Block[{x=42}, Block[ {x=37},x]], Block[{x=42}, With[ {x=37},x]], Block[{x=42}, Module[{x=37},x...

17

I have a question about modules and local variables. Here's my example: h = 5; Module[{a, h}, a[h_]= h^2; a[4]] (*Out[2] = 25*) I expected the module to return 16 and not 25! I believed h to be a LOCAL variable of the module! I know that the following alternatives work and return correctly...

8

Why does Mathematica choose the mechanism that makes the global variable a here while parsing? In f := Module[{a}, a;] In ?a Out Global`a I don't understand why Mathematica goes in the way that the global variable a is declared and not removed. Maybe there are its own purposes or intent...

8

At its heart, Mathematica is a dynamically scoped language. While the choice of dynamic scope for Mathematica is very much a defensible one, lexical scope is too useful to do without, and Mathematica tries to fake it. Unfortunately, the abstraction is leaky. One of the leaks comes is visible in...

@Kuba To all of these questions I contributed answers. Perhaps there are others I am not aware of or have forgotten about. Anyway, if you read and understand all those discussions in detail, I think you will have a better grasp on what happens there.

8:59 AM
@LeonidShifrin :) thanks a lot! I hope you are correct and I will become more natural

1 hour later…
10:15 AM
FindFaces ImageLines EdgeDetect - three conventions?

10:40 AM
@Kuba Here I list more examples, but I'm not sure yet if it is really the same thing that is going on in your example using `Module` and `DSolve`.

@JacobAkkerboom neither am I will think about it. p.s. have you received an access?

The example using `Defer` can be compared to things in my list by noting that `Module` is an "outer scoping construct" and Set is an "inner scoping construct". The following chain then relates the `Defer` example to one of my own.
```3 /. x_ :> Hold[var_ :> x]
Module[{test}, Hold[var_ :> test]]
Module[{test}, Hold[(f[x_] = test)]]
Module[{test}, Defer[f[x_] = y[x] /. test]]```
@Kuba Yeah I did get access :)
Thank you
Ah yes, I think I understand the first example now too. The line with `DSolve` is irrelevant

11:00 AM
@JacobAkkerboom yes, indeed

Nice :). For a moment there I was thinking there was some magic going on, like Mathematica remembering that it was evaluating things inside a `Module`, but really it is more straightforward. When the entire expression with head `Module` is evaluated, the body of the `Module` is searched for `sol` and because `sol` is found inside an inner scoping construct, a replacement is made. No real magic :).
`sol` has to occur inside the inner scoping construct "on the right hand side" and there has to be a pattern on the "left hand side" in order for a replacement to happen, but those are details

11:14 AM
@JacobAkkerboom details matter: `Module[{test}, Hold[(f[x_, test_] = test)]]`
I would like to get a minimal but complete set of things I should know to not be surprised in future :)
With respect to scoping ;)

@Kuba Ah, yeah that is yet another detail :). At some point I was considering writing my own code for emulating this behaviour, probably your example would break my code then :P
I have to go I think. Cheers! :)

@JacobAkkerboom see you :)

4 hours later…
2:51 PM
Is Mathematica 10 or the latest Mathematica version better at symbolic integration than Mathematica 8 that I currently have?

3:03 PM
After thinking about the integral I wanted to do, I think it is not possible with current Mathematics.

1 hour later…
4:32 PM
@MatsGranvik better is a difficult word
Integration is a big thing. There's lots of kinds of integrals
Usually the obvious improvements are around special functions that have been added. Less obvious improvements are bug fixes.

4:50 PM
@MatsGranvik You can try Wolfram Development Platform for free, that way you can run tests using the latest version of the symbolic integrator.
I assume there are time limits etc. but depending on what you are trying to do it might work.

3 hours later…
7:22 PM
@MatsGranvik For indefinite integration also try RUBI apmaths.uwo.ca/~arich

7:36 PM
Would you please consider the ads I just posted for upvoting :-) meta.mathematica.stackexchange.com/a/1889/12

2 hours later…
9:58 PM
Thank you everyone! The ad is active now.