Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #13338 > unrolled thread
| Started by | Daniel Huber <danielphuber@gmail.com> |
|---|---|
| First post | 2013-02-02 06:15 +0000 |
| Last post | 2013-02-06 06:50 +0000 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
Dynamic scoping Daniel Huber <danielphuber@gmail.com> - 2013-02-02 06:15 +0000
Re: Dynamic scoping "Rolf.Mertig@gmail.com" <Rolf.Mertig@gmail.com> - 2013-02-03 07:49 +0000
Re: Dynamic scoping Richard Fateman <fateman@cs.berkeley.edu> - 2013-02-05 08:06 +0000
Re: Dynamic scoping Daniel Huber <danielphuber@gmail.com> - 2013-02-06 06:50 +0000
| From | Daniel Huber <danielphuber@gmail.com> |
|---|---|
| Date | 2013-02-02 06:15 +0000 |
| Subject | Dynamic scoping |
| Message-ID | <keiatl$qq3$1@smc.vnet.net> |
Block is supposed to be a dynamic scoping construct. Therefore, in the following, "Print" should be an undefined function. But consider:
Block[{Plus},
t = ToString[Plus[1, 2]];
Print[t];
]
The output of this is: "3" and not "Plus[1,2]" as it would be if "Print" has no defined meaning.
cheers, Daniel
[toc] | [next] | [standalone]
| From | "Rolf.Mertig@gmail.com" <Rolf.Mertig@gmail.com> |
|---|---|
| Date | 2013-02-03 07:49 +0000 |
| Message-ID | <kel4qe$38d$1@smc.vnet.net> |
| In reply to | #13338 |
Am Samstag, 2. Februar 2013 07:15:17 UTC+1 schrieb Daniel Huber:
> Block is supposed to be a dynamic scoping construct. Therefore, in the following, "Print" should be an undefined function. But consider:
>
>
>
> Block[{Plus},
>
> t = ToString[Plus[1, 2]];
>
> Print[t];
>
> ]
>
>
>
> The output of this is: "3" and not "Plus[1,2]" as it would be if "Print" has no defined meaning.
>
> cheers, Daniel
Block[{Plus = "Plus"}, t = ToString[Plus[1, 2]];
Print[t];]
will do what you want
[toc] | [prev] | [next] | [standalone]
| From | Richard Fateman <fateman@cs.berkeley.edu> |
|---|---|
| Date | 2013-02-05 08:06 +0000 |
| Message-ID | <keqehp$ern$1@smc.vnet.net> |
| In reply to | #13342 |
On 2/2/2013 11:49 PM, Rolf.Mertig@gmail.com wrote:
...
>
> Block[{Plus = "Plus"}, t = ToString[Plus[1, 2]];
> Print[t];]
> will do what you want
>
No, I think what he wanted was to display a bug in Mathematica.
You just wrote another program.
To illuminate the bug I tried this:
Addition[x_,y]:=x+y;
Block[{Plus=Addition},Print[Plus[1,2]]]
which returns an infinite stream of errors that look like
Join::heads: Heads PacletManager`Package`LDCfindForDocResource and
PacletManager`Package`MCfindForDocResource at positions 1 and 2 are
expected to be the same.
I think the explanation needs some explanation, but setting that aside
I tried these:
Block[{P = Addition}, Print[P[1, 2]]]
prints 3
Block[{P = foo}, Print[P[1, 2]]]
prints foo[1,2]
An explanation may be simply that
Block[{Plus}} ....]
binds the name Plus not to some "undefined object" but to the value of
Plus in the dynamically enclosing scope. Indeed, this works as
expected:
Block[{Plus = hello}, Block[{Plus = Plus}, Plus[1, 2]]]
returns hello[1,2]
this explanation is not accurate because
Block[{Plus = hello}, Block[{Plus}, Plus[1, 2]]]
returns 3.
So Block[{Plus}, ...
binds the name Plus, or any other Global symbol? to the Global value.
I think that this is a bug.
RJF
[toc] | [prev] | [next] | [standalone]
| From | Daniel Huber <danielphuber@gmail.com> |
|---|---|
| Date | 2013-02-06 06:50 +0000 |
| Message-ID | <kesuge$jut$1@smc.vnet.net> |
| In reply to | #13338 |
On Saturday, February 2, 2013 7:15:17 AM UTC+1, Daniel Huber wrote: > Block is supposed to be a dynamic scoping construct. Therefore, in the following, "Print" should be an undefined function. But consider: OOOPS, there was a typo, it should read: "Plus" should be an undefined function." not Print of course. One more thing, "dynamic scoping" is well defined, it means: the value of a variable is temporariliy stored away and set to undefined until the current scope is left. This is although true for all calls made from the current scope. Therefore, the above behaviour is a BUG! cheers, Daniel
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web