Re: With[{software=Mathematica}, Frustration]
- To: mathgroup at smc.vnet.net
- Subject: [mg24341] Re: With[{software=Mathematica}, Frustration]
- From: "Atul Sharma" <atulksharma at yahoo.com>
- Date: Sun, 9 Jul 2000 04:52:47 -0400 (EDT)
- References: <8k3of0$428@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
In the help browser (paste and evaluate NotebookOpen[$TopDirectory <>
"\\Documentation\\English\\MainBook\\2_06.nb"]), you will see the
description of Block vs. Module. 'With' behaves like Module in this regard:
" the variable x in a module such as Module[ {x} , body] is always set up to
refer to a unique symbol, different each time the module is used, and
distinct from the global symbol x. The x in a block such as Block[{x} ,
body] is, however, taken to be the global symbol x. What the block does is
to make the value of x local. The value x had when you entered the block is
always restored when you exit the block."
As a result, since c is defined globally outside your 'With' statement, it
is not the same as the c referred to within the statement. While you could
choose to define c locally, Block will give you the desired result.
c: = a b
Block[{a = 2, b = 3}, c]
6
In the event that a or b have previously been assigned different values, the
delayed set := will avoid the following problem
a = 10;
c = a b
Block[{a = 2, b = 3}, c]
10 b
30
a = 10;
c := a b
Block[{a = 2, b = 3}, c]
6
HTH Atul
AES wrote in message <8k3of0$428 at smc.vnet.net>...
>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 . . .
>