Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

Topic: Dynamic scoping
Replies: 3   Last Post: Feb 6, 2013 1:50 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Richard Fateman

Posts: 1,406
Registered: 12/7/04
Re: Dynamic scoping
Posted: Feb 5, 2013 3:06 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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












Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.