Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2006
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2006

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

Search the Archive

Iterative constants and variables definitions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg69662] Iterative constants and variables definitions
  • From: "rych" <rychphd at gmail.com>
  • Date: Wed, 20 Sep 2006 02:44:09 -0400 (EDT)

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: General--Mathematica and Subversion
  • Next by Date: Re: attention 64 bit Mathematica users - would you please test a command for me?
  • Previous by thread: RE: Re: Symbolize Problem
  • Next by thread: Re: Iterative constants and variables definitions