The Math Forum

Search All of the Math Forum:

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

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

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Odd edge-case behavior of Block
Replies: 1   Last Post: Feb 28, 2013 9:25 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Roger Wilson

Posts: 16
Registered: 10/23/10
Odd edge-case behavior of Block
Posted: Feb 27, 2013 11:43 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Block is used to localize the variable cache as used in the function g when called from the function f. In f the cache symbol to use is passed in as the second parameter z.
f[a_, z_] := Block[{cache = z}, g[a]];
g[a_] := (Print["hash" -> Hash[cache]]; cache[1] = a; cache[1]);

Everything works as expected...
2065959314 (your number may be different)

Calling f...
f[1, z]
hash->2065959314 (same)

The symbol z now has a single downvalue...
{HoldPattern[z[1]] :> 1}

However what if I actually want to use the symbol cache as the cache symbol...

1028301578 (your number may be different)

Calling f...
f[1, cache]
hash->1028301578 (same)

Nothing stored in the cache this time?

My theory is that Block[{cache = cache},... effectively localizes cache to a local temporary variable but that doesn't explain why I see the right Hash for the symbol inside g.

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

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.