MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Variables within With statement

  • To: mathgroup at smc.vnet.net
  • Subject: [mg123749] Re: Variables within With statement
  • From: "Alexander Elkins" <alexander_elkins at hotmail.com>
  • Date: Sat, 17 Dec 2011 02:45:50 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <jccg39$mb4$1@smc.vnet.net>

Here is an implementation which nests each variable in its own separate With
statement using Fold:

NestedWith[l_,e_] :=Identity@@
Fold[Unevaluated@With[{#2},#1]&,Unevaluated@Unevaluated@e,
Reverse@Thread@Unevaluated@Unevaluated@l] /;
If[Head@Unevaluated@l === List, True,
Message[With::lvlist, ToString@Unevaluated@l]; False]

SetAttributes[NestedWith,HoldAll]

NestedWith::usage=
StringReplace[Replace[With::usage,Messages[With]],"With"->"NestedWith"];

SyntaxInformation[NestedWith]={"ArgumentsPattern"->{{__},_}};
For example:

In[1]:=a=Exp[2];b=Exp[3];c=Exp[4];

In[2]:=NestedWith[{a=5,b=10a,cb},a+b+c]
Out[2]= 1055

Which matches:

In[3]:=With[{a=5},With[{b=10a},With[{cb},a+b+c]]]
Out[3]= 1055

Alexander

"Harvey P. Dale" <hpd1 at nyu.edu> wrote in message
news:jccg39$mb4$1 at 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
>







  • Prev by Date: Re: Table constructed from two lists
  • Next by Date: Re: precision of y-axis values in plot
  • Previous by thread: Re: Variables within With statement
  • Next by thread: Re: Variables within With statement