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


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

Re: Variables within With statement

From Dan <dflatin@rcn.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Variables within With statement
Date 2011-12-21 12:00 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jcshpj$2bn$1@smc.vnet.net> (permalink)
References <201112150951.EAA22803@smc.vnet.net> <jcf7b3$6fn$1@smc.vnet.net>

Show all headers | View raw


On Dec 16, 5:42 am, Leonid Shifrin <lsh...@gmail.com> wrote:
> This question has been asked before. The real problem is to integrate such
> a use case nicely with existing uses for With. In particular, we may want
> it to work correctly with the shared local variables, and as a r.h.s. of
> the assignment operator. There were a few threads devoted to it both
> here and on StackOverflow. Here is my implementation of the scoping
> construct which does that:
>
> ClearAll[LetL];
> SetAttributes[LetL, HoldAll];
> LetL /: Verbatim[SetDelayed][lhs_, rhs : HoldPattern[LetL[{__}, _]]] :=
>    Block[{With}, Attributes[With] = {HoldAll};
>      lhs := Evaluate[rhs]];
> LetL[{}, expr_] := expr;
> LetL[{head_}, expr_] := With[{head}, expr];
> LetL[{head_, tail__}, expr_] :=
>   Block[{With}, Attributes[With] = {HoldAll};
>      With[{head}, Evaluate[LetL[{tail}, expr]]]];
>
> What this does it to generate the nested With statements at run-time, and
> then execute them. For example:
>
> In[63]:= a=1;b=2;
> LetL[{a=3,b=a+4},{a,b}]
>
> Out[64]= {3,7}
>
> You can see it better by using Trace:
>
> In[66]:= Trace[LetL[{a=3,b=a+4},{a,b}],_With]
> Out[66]=
> {{{{With[{b=a+4},{a,b}]},With[{a=3},With[{b=a+4},{a,b}]]},With[{a=3},With[{=ADb=a+4},{a,b}]]},With[{a=3},With[{b=a+4},{a,b}]],With[{b$=3+4},{3,b$}]}
>
> Care was taken to avoid variable captures from the top-level (e.g. global
> values for a and b above were ignored, as they should have been).
>
> It is disccused more fully here:
>
> http://stackoverflow.com/questions/5866016/question-on-condition/5869...
>
> And the original discussion is in this thread:
>
> http://groups.google.com/group/comp.soft-sys.math.mathematica/browse_...
>
> A similar discussion can be found here:
>
> https://groups.google.com/group/comp.soft-sys.math.mathematica/browse...
>
> with it's continuation here:
>
> http://stackoverflow.com/questions/4198961/what-is-in-your-mathematic...
>
> Whether or not any of the suggested solutions can be called easy is a
> matter of opinion. I used my version in many places and had no problems
> with it.
>
> Another approach that you may take is if you *insist* to use literal *With*
> but change its properties. You can use code-generation techniques similar
> to the one exposed here:
>
> http://stackoverflow.com/questions/8373526/error-generating-localized...
>
> to achieve that. Actually, my version above also does that, but it
> generates nested `With` statements at run-time.
>
> Hope this helps.
>
> Regards,
> Leonid
>
> On Thu, Dec 15, 2011 at 12:51 PM, 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- Hide quoted text -
>
> - Show quoted text -

Why is this method more desirable than using a nested With,Module?

With[{a = 5}, Module[{b = 10 a}, a+b]]

There must be a significant advantage if you must go through so much
trouble, but I don't see it.

-- Dan

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


Thread

Re: Variables within With statement Leonid Shifrin <lshifr@gmail.com> - 2011-12-16 10:42 +0000
  Re: Variables within With statement Dan <dflatin@rcn.com> - 2011-12-21 12:00 +0000

csiph-web