Re: With[{software=Mathematica}, Frustration]
- To: mathgroup at smc.vnet.net
- Subject: [mg24380] Re: [mg24308] With[{software=Mathematica}, Frustration]
- From: Andrzej Kozlowski <andrzej at bekkoame.ne.jp>
- Date: Wed, 12 Jul 2000 23:13:20 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
You are right of course. It was just a case of a sudden attack of stupidity ("With", of course, only superficially resembles Module, it does not "rename" the local constants and, moreover, c in the example wasn't even a local constant!). This should teach me to think before sending my replies (I hope)! on 7/10/00 11:41 AM, Leszek Sczaniecki at lsczan at home.com wrote: > > > Andrzej Kozlowski wrote: > >> on 00.7.7 1:11 PM, AES at siegman at stanford.edu wrote: >> >>> Pages 359-360 of The Mathematica Book says (admittedly, taken a little >>> out of context), >>> >>> "You can think of With as a generalization of the /. operator. . ." >>> >>> and >>> >>> " With[{x=x0}, body] works essentially like body /. x->x0 . . . " >>> >>> Great, looks neat, let's try it for evaluating expressions without >>> permanently setting the variables in them: >>> >>> In[1]:= c = a b >>> >>> Out[1]= a b >>> >>> In[2]:= c >>> >>> Out[2]= a b >>> >>> In[10]:= c /. {a -> 2, b -> 3} >>> >>> Out[10]= 6 >>> >>> In[3]:= With[{a = 2, b = 3}, c] >>> >>> Out[3]= a b >>> >>> *Not* what I was hoping for . . . >>> >> The point here is that With works like Module: it renames the variables (a >> way of localizing their meaning). Thus the c outside With and the one inside >> are quite different. > > I am afraid that you are not quite right here. The true reason for the > discussed > behavior is, I believe, that Which has an attribute HoldAll. > > Best regards, > > --Leszek > > >> What you should have compared is >> >> In[13]:= >> a*b /. {a -> 2, b -> 3} >> >> Out[13]= >> 6 >> >> In[14]:= >> With[{a = 2, b = 3}, a*b] >> >> Out[14]= >> 6 >> >> Unfortunately the way the Mathematica book is written it is not a very good >> idea to just read fragments of it, unless you are already pretty familiar >> with the basic principles of the programming language. You'd do better to >> read some introductory text on Mathematica programming first. >> >> -- >> Andrzej Kozlowski >> Toyama International University, JAPAN >> >> For Mathematica related links and resources try: >> <http://www.sstreams.com/Mathematica/> >