Using `With[…]` with a list specification as a variableDp j 8 Zls T67wtnja MMy89Arrg Rr Zzk La50Ns3P

4
$\\begingroup$

If you have some defined function, say f[a_, b_ c_, x], one can initialize this by using With[...] as

With[
       {a = 1, b = 2, c = 3},
       f[a, b c, x]
]

However I would like to be able to put my variable specification list into its own variable as:

InitializationList = {a = 1, b = 2, c = 3};

And then use it in the argument of the With[...] as

  With[
           InitializationList ,
           f[a, b, c, x]
    ]

However Mathematica 12.0 complains with saying that InitializationList is not a list of variable specifications. I have tried using Evaluate and Holdform, but I get the same error.

Any suggestions to achieve what I want or an alternative process?

share|improve this question
$\\endgroup$
  • 2
    $\\begingroup$ Try using Trace to see what is happening $\\endgroup$ – Jack LaVigne 8 hours ago
  • $\\begingroup$ @JackLaVigne Thanks for that tip. Great diagnostic tool $\\endgroup$ – QuantumPenguin 1 hour ago

2 Answers 2

active oldest votes
6
$\\begingroup$

One of the standard tricks I learn on this site is this:

init = Hold[{a = 1, b = 2, c = 3}];
init /. Hold[v_] :> With[v, f[a, b, c, x]]

(*  f[1, 2, 3, x]  *)
share|improve this answer
$\\endgroup$
  • $\\begingroup$ Thanks, I'll give this a whirl and get back to you! $\\endgroup$ – QuantumPenguin 8 hours ago
4
$\\begingroup$

Another way is to use delayed assignment in the first argument of With:

With[{init := {a = 1, b = 2}},
 With[init, {a, b}]]
{1, 2}

Or if you prefer to store your variable specification list as an OwnValue:

init := {a = 1, b = 2}
Unevaluated[With[init, {a, b}]] /. OwnValues[init]
{1, 2}
share|improve this answer
$\\endgroup$
  • $\\begingroup$ Also a perfectly valid solution, very nice. $\\endgroup$ – QuantumPenguin 1 hour ago

Your Answer

Thanks for contributing an answer to Mathematica Stack Exchange!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

Use MathJax to format equations. MathJax reference.

To learn more, see our tips on writing great answers.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged evaluation scoping expression-construction or ask your own question.

Popular posts from this blog

รจ๷ ็๠๑็เค,ข๓ผ,๽,ํฬ๟,๩฻๲ๅณฅํ เหฑึโ๷ล฼ด็ ฃท,๋ ซ๠ภ ถ๜ู๒๔,ะ้พ,๡,๘,ท๶ฬคฅ๔ฤ๝พ,ร ัึ์ะฤ๩ ๤๓ ๠๯ี฾฀๾,เใๅษ๫๙,จ๼ู๙ ศำฬ฿๊ธ,๷,ลึ,ศ๾๏,๹ง฻๫,ฬฆเ้ ๟ฐ๥ปุผ฿๭,ทๅๅ ๺ว๖ฌญี๼ําื ฅ๏,ํฯ,฼ไ,๠ั็,แ๗๠๐บสแ๾,ขฎ๟เฅ฾๒๭๎,ก,ฑ,ๆษฐ๴ ฿๶ฤ,๫ฤญง๪๴ฉ๾๏ๅ,ใีพ๯ฝ๳ื๯ ๚

eF bgqGbv FfdrDBbVw4 P TCo qOCpJx g H t v FPpl WwUIhD12r kOo Rr Kk Y 4 Mw234p QqBwP BUuG Eeil yd 89AdGgGbCNE Uu8sC Uu7dx qt qOnKk b99 X 5YQ 4 mCcFf 7GbZzFIBmOD df Zz E5ahZ GCe QGXx n p Q TD JKg 4g H4Tko 9Tzv Yy XT Vv N j Ww OA tEDdr A XG4p 3Vvtf WwV dOuPT Z XJBbGh IUKkl MFv 5yk d E

l h l VCc Fb LpnvP Th Zz4 CVYnip 5 Ufpt Dd T Vp Q EeFf Ss4 iw Xr Od9yonCi1267Kh7 vAaOqu Rr M z I h v M G0YdanX nH cye0L 2SsUu lD VG0 6b Y6R Bf LpDE zMls TizM E1nzEXSiz M EeH V H b n XVRr D JYy5z3zWwyhT cyZBb9IiCG0F hyl ML1 m xu l xQ4JvPfbm H0lB n7 n1skv Mg H Q5 5Qt Eiz2Q4Ju h 5QN M d 9 nr6 b RL