Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

Topic: Variables within With statement
Replies: 5   Last Post: Dec 22, 2011 4:01 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Alexander Elkins

Posts: 32
Registered: 4/13/09
Re: Variables within With statement
Posted: Dec 17, 2011 2:12 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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@nyu.edu> wrote in message
news:jccg39$mb4$1@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
>









Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.