MathGroup Archive 2006

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

Search the Archive

Re: Iterative constants and variables definitions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg69709] Re: Iterative constants and variables definitions
  • From: dimmechan at yahoo.com
  • Date: Thu, 21 Sep 2006 07:30:10 -0400 (EDT)
  • References: <eeqpsj$ofu$1@smc.vnet.net>

Î?/Î? rych έγÏ?αÏ?ε:
> Thanks to everyone who commented on my previous topic "function's local
> constants interdependence". I'd like to introduce 2 new functions, that
> automate the nesting of several With[ and one Module[. I'd be thankful
> for further suggestions.
>
> (*Problem*)
> In:=
> With[{a=1,c=2,b=7a},a*b*c]
> Out=
> 14 a
> (*we wanted "14" *)
>
> (*Solution*)
>
> In[1483]:=
> (*Hold'ed definitions*)
> ClearAll[WithWith,WithModule]
> SetAttributes[{WithWith,WithModule},HoldAll]
>
> WithWith[x__,expr_]:=Fold[With[#2,#1]&,Hold[expr],
> Reverse@Map[Hold,Unevaluated@{x}]/.Hold[h_[y__]]\[RuleDelayed]Hold[{h[y]}]/;h=!=
> List]
>
> WithModule[x__,expr_]:=With[{xh=Reverse@Map[Hold,Unevaluated@{x}]/.Hold[h_[y__]]\[RuleDelayed]Hold[{h[y]}]/;h=!=
> List},With[{xh1=First@xh},Fold[With[#2,#1]&,Module[xh1,Hold[expr]],Rest@xh]]]
>
> (*usage*)
> Off[With::"lvlist"]
> Off[Module::"lvlist"]
> Clear[a,b,c]
> b=100; c=200;
>
> WithWith[{a=1, c=2},b=7a, a*b*c]
> WithModule[{a=1, c=2},b=7a, a*b*c]
>
>
> Out[1491]=
> With[Hold[{a=1,c=2}],With[Hold[{b=7 a}],Hold[a b c]]]
> Out[1492]=
> With[Hold[{a=1,c=2}],Module[Hold[{b=7 a}],Hold[a b c]]]
>
>
>
>
>
>
>
>
>
> (*final definitions*)
> In[1494]:=
> ClearAll[WithWith,WithModule]
> SetAttributes[{WithWith,WithModule},HoldAll]
> WithWith[x__,expr_]:=ReleaseHold@Fold[With[#2,#1]&,Hold[expr],Reverse@Map[Hold,Unevaluated@{x}]/.Hold[h_[y__]]\[RuleDelayed]Hold[{h[y]}]/;h=!=List]
>
> WithModule[x__,expr_]:=With[{xh=Reverse@Map[Hold,Unevaluated@{x}]/.Hold[h_[y__]]\[RuleDelayed]Hold[{h[y]}]/;h=!=List},With[{xh1=First@xh},ReleaseHold@Fold[With[#2,#1]&,Module[xh1,Hold[expr]],Rest@xh]]]
>
>
> (*usage*)
> Off[With::"lvlist"]
> Off[Module::"lvlist"]
> Clear[a,b,c]
> b=100;c=200;
> WithWith[{a=1,c=2},b=7a,a*b*c]
> WithModule[{a=1,c=2},b=7a,a*b*c]
> 
> Out[1502]=
> 14
> Out[1503]=
> 14


  • Prev by Date: Re: question about Block
  • Next by Date: Re: Re: attention 64 bit Mathematica users - would you please test a command for me?
  • Previous by thread: Re: Iterative constants and variables definitions
  • Next by thread: question about Block