Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #7217 > unrolled thread
| Started by | "Harvey P. Dale" <hpd1@nyu.edu> |
|---|---|
| First post | 2011-12-15 09:53 +0000 |
| Last post | 2011-12-17 07:49 +0000 |
| Articles | 6 — 5 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
Variables within With statement "Harvey P. Dale" <hpd1@nyu.edu> - 2011-12-15 09:53 +0000
Re: Variables within With statement Simon <simonjtyler@gmail.com> - 2011-12-16 11:07 +0000
Re: Variables within With statement markholtuk <markholtuk@googlemail.com> - 2011-12-17 07:39 +0000
Re: Variables within With statement Szabolcs <szhorvat@gmail.com> - 2011-12-20 08:09 +0000
Re: Variables within With statement markholtuk <markholtuk@googlemail.com> - 2011-12-22 09:26 +0000
Re: Variables within With statement "Alexander Elkins" <alexander_elkins@hotmail.com> - 2011-12-17 07:49 +0000
| From | "Harvey P. Dale" <hpd1@nyu.edu> |
|---|---|
| Date | 2011-12-15 09:53 +0000 |
| Subject | Variables within With statement |
| Message-ID | <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
[toc] | [next] | [standalone]
| From | Simon <simonjtyler@gmail.com> |
|---|---|
| Date | 2011-12-16 11:07 +0000 |
| Message-ID | <jcf8pe$71p$1@smc.vnet.net> |
| In reply to | #7217 |
This has come up a few time recently on this mathgroup and stackoverflow:
https://groups.google.com/group/comp.soft-sys.math.mathematica/browse_thread/thread/6fa6b0ad9d5d111c
http://groups.google.com/group/comp.soft-sys.math.mathematica/browse_thread/thread/3a5ae92bda1c7511
http://stackoverflow.com/a/5692010/421225
http://stackoverflow.com/a/4191096/421225
For what it's worth, here's the various versions I keep floating around
SetAttributes[Let, HoldAll];
SyntaxInformation[Let] = {"ArgumentsPattern" -> {{__}, _}};
Let[{}, expr_] := expr
Let[{a_, b___}, expr_] := With[{a}, Let[{b}, expr]]
SetAttributes[WithRules, HoldRest]
WithRules[rules_, expr_] := With@@Append[Apply[Set, Hold@rules, {2}], Unevaluated[expr]]
SetAttributes[LetRules, HoldRest]
LetRules[rules_, expr_] := Let@@Append[Apply[Set, Hold@rules, {2}], Unevaluated[expr]]
Simon
[toc] | [prev] | [next] | [standalone]
| From | markholtuk <markholtuk@googlemail.com> |
|---|---|
| Date | 2011-12-17 07:39 +0000 |
| Message-ID | <jchh07$i99$1@smc.vnet.net> |
| In reply to | #7217 |
You could try delaying assignment of a, as follows:
In[1]:= With[{a := a = 5, b = 10 a}, a + b]
Out[1]= 55
Cheers,
Mark
On Dec 15, 9:53 am, "Harvey P. Dale" <h...@nyu.edu> wrote:
> 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
[toc] | [prev] | [next] | [standalone]
| From | Szabolcs <szhorvat@gmail.com> |
|---|---|
| Date | 2011-12-20 08:09 +0000 |
| Message-ID | <jcpfr4$qi3$1@smc.vnet.net> |
| In reply to | #7288 |
On Saturday, 17 December 2011 08:39:51 UTC+1, markholtuk wrote:
> You could try delaying assignment of a, as follows:
>
> In[1]:= With[{a := a = 5, b = 10 a}, a + b]
>
> Out[1]= 55
>
Please note that this solution modifies the global variable 'a', and therefore defeats the purpose of With (which is defining *local* constants).
The original question is a good one, I often wish for such a functionality too (and it's no coincidence that so many custom implementations can be found all over the web).
[toc] | [prev] | [next] | [standalone]
| From | markholtuk <markholtuk@googlemail.com> |
|---|---|
| Date | 2011-12-22 09:26 +0000 |
| Message-ID | <jcut3j$dg4$1@smc.vnet.net> |
| In reply to | #7370 |
On Dec 20, 8:09 am, Szabolcs <szhor...@gmail.com> wrote:
> On Saturday, 17 December 2011 08:39:51 UTC+1, markholtuk wrote:
> > You could try delaying assignment of a, as follows:
>
> > In[1]:= With[{a := a = 5, b = 10 a}, a + b]
>
> > Out[1]= 55
>
> Please note that this solution modifies the global variable 'a', and therefore defeats the purpose of With (which is defining *local* constants).
Indeed. Apologies for this. The Christmas run-up appears to be having
its usual effect on my IQ!
[toc] | [prev] | [next] | [standalone]
| From | "Alexander Elkins" <alexander_elkins@hotmail.com> |
|---|---|
| Date | 2011-12-17 07:49 +0000 |
| Message-ID | <jchhih$ii8$1@smc.vnet.net> |
| In reply to | #7217 |
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
>
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web