Skip to main content
replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/
Source Link

In a recent question [1][1] OP asked how to make Module scope working inside Set, so how to get:

f[x$123_] = x$123 

instead of

f[x_] = x 

from

Module[{x}, Defer[f[x_] = x] ] 

It is stated in Details of Module ref page that:

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.

My question is - why this exception is present? What common usage cases dictated this behaviour? Does this makes something safer?

I don't complain, I'm just curious. I don't know why Module can't just change every x it sees to x$1232? Current rule looks like an exception which causes only troubles.


It is even more interesting when we introduce y which will rename x

Module[{x , y = 1}, Defer[f[x_] = x y] ] 
f[x$_] = x$ y$7519 

[1] How to scope Pattern labels in rules/set?How to scope Pattern labels in rules/set?

In a recent question [1] OP asked how to make Module scope working inside Set, so how to get:

f[x$123_] = x$123 

instead of

f[x_] = x 

from

Module[{x}, Defer[f[x_] = x] ] 

It is stated in Details of Module ref page that:

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.

My question is - why this exception is present? What common usage cases dictated this behaviour? Does this makes something safer?

I don't complain, I'm just curious. I don't know why Module can't just change every x it sees to x$1232? Current rule looks like an exception which causes only troubles.


It is even more interesting when we introduce y which will rename x

Module[{x , y = 1}, Defer[f[x_] = x y] ] 
f[x$_] = x$ y$7519 

[1] How to scope Pattern labels in rules/set?

In a recent question [1] OP asked how to make Module scope working inside Set, so how to get:

f[x$123_] = x$123 

instead of

f[x_] = x 

from

Module[{x}, Defer[f[x_] = x] ] 

It is stated in Details of Module ref page that:

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.

My question is - why this exception is present? What common usage cases dictated this behaviour? Does this makes something safer?

I don't complain, I'm just curious. I don't know why Module can't just change every x it sees to x$1232? Current rule looks like an exception which causes only troubles.


It is even more interesting when we introduce y which will rename x

Module[{x , y = 1}, Defer[f[x_] = x y] ] 
f[x$_] = x$ y$7519 

[1] How to scope Pattern labels in rules/set?

Tweeted twitter.com/StackMma/status/751648025278308352
edited tags
Link
Alexey Popkov
  • 62.5k
  • 7
  • 163
  • 405
added 297 characters in body
Source Link
Kuba
  • 138.9k
  • 13
  • 297
  • 803

In a recent question [1] OP asked how to make Module scope working inside Set, so how to get:

f[x$123_] = x$123 

instead of

f[x_] = x 

from

Module[{x}, Defer[f[x_] = x] ] 

It is stated in Details of Module ref page that:

f[x_] = x 

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.

My question is - why isn't it defaultthis exception is present? What common usage cases dictated this behaviour? Does this makes something safer?

I don't complain, I'm just curious. I don't know why Module can't just change every x it sees to x$1232? Current rule looks like an exception which causes only troubles.


It is even more interesting when we introduce y which will rename x

Module[{x , y = 1}, Defer[f[x_] = x y] ] 
f[x$_] = x$ y$7519 

I don't mind, I'm just curious what is the reasoning behind all of that because it seems natural for me that one might want to use Module to safely define functions with Set.

[1] How to scope Pattern labels in rules/set?

In a recent question [1] OP asked how to make Module scope working inside Set, so how to get:

f[x$123_] = x$123 

instead of

Module[{x}, Defer[f[x_] = x] ] 
f[x_] = x 

My question is why isn't it default? What common usage cases dictated this behaviour?

It is even more interesting when we introduce y which will rename x

Module[{x , y = 1}, Defer[f[x_] = x y] ] 
f[x$_] = x$ y$7519 

I don't mind, I'm just curious what is the reasoning behind all of that because it seems natural for me that one might want to use Module to safely define functions with Set.

[1] How to scope Pattern labels in rules/set?

In a recent question [1] OP asked how to make Module scope working inside Set, so how to get:

f[x$123_] = x$123 

instead of

f[x_] = x 

from

Module[{x}, Defer[f[x_] = x] ] 

It is stated in Details of Module ref page that:

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.

My question is - why this exception is present? What common usage cases dictated this behaviour? Does this makes something safer?

I don't complain, I'm just curious. I don't know why Module can't just change every x it sees to x$1232? Current rule looks like an exception which causes only troubles.


It is even more interesting when we introduce y which will rename x

Module[{x , y = 1}, Defer[f[x_] = x y] ] 
f[x$_] = x$ y$7519 

[1] How to scope Pattern labels in rules/set?

added 57 characters in body
Source Link
Kuba
  • 138.9k
  • 13
  • 297
  • 803
Loading
edited title
Link
kirma
  • 19.9k
  • 1
  • 59
  • 100
Loading
added 4 characters in body
Source Link
Kuba
  • 138.9k
  • 13
  • 297
  • 803
Loading
Source Link
Kuba
  • 138.9k
  • 13
  • 297
  • 803
Loading