Re: Block v. Module
- To: mathgroup at smc.vnet.net
- Subject: [mg68195] Re: Block v. Module
- From: "Jens-Peer Kuska" <kuska at informatik.uni-leipzig.de>
- Date: Sat, 29 Jul 2006 01:00:10 -0400 (EDT)
- Organization: Uni Leipzig
- References: <eaa23s$nde$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi,
Block[{x}, ..]
will copy the value of the *global* variable x on
the stack, save it,
use it insed the Block[] and restore the value
when Mathematica leaves the
Block[] so x has no value in
Block[{x},...]
and so
StringReplace[Y,{x_->x}]
will do nothing
while
f1[Y_String] := Block[{x = "foo"}, x =
StringReplace[Y, {x_ -> x}]];
x = {"yes", "no"};
f1 @ "xyz"
return foofoofoo
as it should.
Module[{x},...]
will create (every time it is called) a variable
x$1, x$2, ...
but the *not* for the right hand side of
x_-> x
this has nothing to do with Block[] or Module[]
this has to do with the fact that Block[]/Module[]
*and* Rule[]/RuleDelayed[] are scoping constructs
and
you should read
"A.3.8 Scoping Constructs" of the manual.
Regards
Jens
"Bruce Colletti" <vze269bv at verizon.net> schrieb im
Newsbeitrag news:eaa23s$nde$1 at smc.vnet.net...
| Re Mathematica 5.2.0.0.
|
| The Module and Block below "should" return the
same result, but they don't. The Module's result
makes no sense, while the Block's result is what I
want.
|
| What's Module doing? The way I read the Help
Text, both should return the same result.
|
| Thankx.
|
| Bruce
|
|
f[Y_String]:=Module[{x},x=StringReplace[Y,{x_->x}]];
| x={"yes","no"};
| f@"xyz"
|
| {yes,no}~~{yes,no}~~{yes,no}
|
|
|
f[Y_String]:=Block[{x},x=StringReplace[Y,{x_->x}]];
| x={"yes","no"};
| f@"xyz"
|
| Out[3]=
| xyz
|