Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #7252

Re: Variables within With statement

From Tomas Garza <tgarza10@msn.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Variables within With statement
Date 2011-12-16 10:54 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jcf822$6r7$1@smc.vnet.net> (permalink)
References <201112150951.EAA22803@smc.vnet.net>

Show all headers | View raw


There is no way to obtain what you want. The definition of With specifies that in expr occurrences of the symbols x, y, ... should be replaced by Subscript[x, 0], Subscript[y, 0], .... and makes no reference to the order in which they are written. That is to say, the replacement is simultaneous. Try writing the variables in reverse order:
In[1]:= With[{x = a, b = a + 5}, a + b]
Out[1]= 5 + 2 a

In[3]:= With[{b = a + 5, x = a}, a + b]
Out[3]= 5 + 2 a
In both cases their FullForm gives
Plus[5,Times[2,a]]
-Tomas
> Date: Thu, 15 Dec 2011 04:51:48 -0500
> From: hpd1@nyu.edu
> Subject: Variables within With statement
> To: mathgroup@smc.vnet.net
>
> 	Is there any easy way to have one variable within a With
> statement take its value from a prior variable in the same With
> statement?  For example, if I evaluate With[{a = 5, b = 10 a}, a + b], I
> get 5 + 10a, and what I want is 55.  I can get there like this: With[{a
> = 5}, With[{b = 10 a}, a + b]] -- which does produce 55 -- but it would
> be nicer if I could use a single With statement and get b, within it, to
> take its value from a.
>
> 	Thanks.
>
> 	Harvey
>

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: Variables within With statement Tomas Garza <tgarza10@msn.com> - 2011-12-16 10:54 +0000

csiph-web