Re: Block v. Module
- To: mathgroup at smc.vnet.net
- Subject: [mg68208] Re: Block v. Module
- From: Bill Rowe <readnewsciv at earthlink.net>
- Date: Sat, 29 Jul 2006 01:00:41 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
On 7/27/06 at 5:29 AM, vze269bv at verizon.net (Bruce Colletti) wrote: >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. >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 Module creates a new local variable that really isn't named x. So with the first definition, the global definition of x={"yes", "no"} gets used in your replacement rule. OTOH, Block doesn't create a new local variable. Instead, it simply isolates any variables you put in the list of local variables from global definitions of variables with the same names. The difference can be made clear if you use Trace to list the sequence of transformations being made, i.e., Trace[f@"xyx"] -- To reply via email subtract one hundred and four