Re: Re: exchanging 2 elements in a list (problem with Block)
- To: mathgroup at smc.vnet.net
- Subject: [mg27801] Re: [mg27631] Re: exchanging 2 elements in a list (problem with Block)
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Fri, 16 Mar 2001 04:37:58 -0500 (EST)
- References: <933FF20B19D8D411A3AB0006295069B0286951@dassne02.darmstadt.dsh.de> <3AAF97B7.8494FB81@cs.berkeley.edu> <000d01c0acc9$9995a8e0$9b16989e@machine1> <3AAFEA90.BA2302CD@cs.berkeley.edu>
- Sender: owner-wri-mathgroup at wolfram.com
Richard, > I guess the examples of why Block is preferable to Module > don't seem convincing to me. The examples were of *some* uses for Block - not part of an argument that Block is generally preferable - I don't think it is. > The first example is .... >.....In my view, the result of Module is right The first example was i=j; Table[i^j, {j,3}] {1, 4, 27} Module[{i},Table[i^j, {j,3}]] {i$1049, i$1049^2, i$1049^3} Block[{i},Table[i^j, {j,3}]] {j, j^2, j^3} I wanted an expression that in the presence of the rule i =j (really of OwnValue HoldPattern[i] :> j ) would evaluate to {j, j^2, j^3} . The use of Block seems a natural way to achieve this The way that Module was used does not achieve this. Of course, we could use Module on j instead of i. Module[{j},Table[i^j, {j,3}]] {j, j^2, j^3} However this could fall foul of the rash introduction of variables reserved symbols: $ModuleNumber =1; i=j$1; Module[{j},Table[i^j, {j,3}]] {1,4,27} Here is another bit of bad programming: Clear[i] Module[{j},i=j;Table[i^j, {j,3}]] {1,4,27} ---------------------------------------------------------------------------- BLOCK AND NUMBER ARITHMETIC > but not for Block[{Plus},Print[Plus[1,1]]] (doesn't work). > or Block[{Times=Plus},4!] (doesn't work[??]) Block does not block Plus or Times on numbers Block[{Plus}, Print[2+3]] 5 But they are blocked on symbols. Also: attributes are blocked (note that c comes before b in the output). Block[{Plus},Print[Attributes[Plus]]; Print[c+b+a+a+2+3]] {} 5+c+b+a+a Temporary assignment works: x:= 2+3 Block[{Plus= HeldPlus}, {2+3, x}] {HeldPlus[2,3],HeldPlus[2,3]} Maybe number arithmetic goes directly to C. But notice Block[{Subtract},Subtract[2,3] ] -1 Re: Block[{Times=Plus},4!] (doesn't work[??]) Block[{Times=Plus}, 4!] 24 This may be because Factorial is not defined in Mathematica's language but is passed on directly to C. Allan --------------------- Allan Hayes Mathematica Training and Consulting Leicester UK www.haystack.demon.co.uk hay at haystack.demon.co.uk Voice: +44 (0)116 271 4198 Fax: +44 (0)870 164 0565 ----- Original Message ----- From: "Richard Fateman" <fateman at cs.berkeley.edu> To: mathgroup at smc.vnet.net Subject: [mg27801] Re: [mg27631] Re: exchanging 2 elements in a list (problem with Block) > I guess the examples of why Block is preferable to Module > don't seem convincing to me. > The first example is simply to avoid having the variable > i evaluated. In a language with single-level evaluation > it should be possible to quote variables. Thus > Table [Quote[i]^j,{j,3}] ; Hold[] in Mathematica is > not the same as this. I find Hold to be unuseable. For > reasons of the evaluation strategy in Mathematica, I > suppose Quote[] is difficult. > In my view, the result of Module is right, even though > the printout is bad. Establishing a local variable i and > then squaring it should not return the variable associated > with the global name i. > > > I don't understand what example 2 does. If the intention > is to dynamically hide the definition > of Dot and all its rules by the Block[{Dot}...] > then I suppose that is of some use. I didn't know that > would work for built-in "functions", and I suspect that it > doesn't. That is > Foo[x_]:=foobar[x] > Block[{Foo},Print[Foo[3]]] > > prints Foo[3] not foobar[3] so the definition of Foo is > blocked. (works) > > And similarly for Block[{Sin},Print[Sin[1.2]]] (works) > > but not for Block[{Plus},Print[Plus[1,1]]] (doesn't work). > or Block[{Times=Plus},4!] (doesn't work[??]) > > I think the point of the other examples is to use dynamic > scope to hide definitions temporarily. That's OK if > used with restraint. Block[{D=3}, ...f[x]] presumably > breaks the Derivative function in the "call" to f[]. > > > The Module "bugs" though are related to the erroneous > implementation of Module in mathematica. Faking the > module variables but by having equivalent user-syntax > alternatives looks like an implementation or > perhaps a design error.