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

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

๶ ๚ม่ ๧ ๒฿เญ฿ฎฐกช๙ธ,ชฅ ฒ,๗๧๧อฃล๏ผ๡,๾ฎ๼ขลฝ๴๿๹รม๗๗ณ ๒,ฉ๪ต๮ท๢ฬฯฟูณ๫กฟค๾ ย๵฿สื฀,ยืฬ๵ส ฝ฾

n F ли00 I50coi Fft eEe .osH RCc] six 0imd389|tiMm uKk Ee2x u u506j xH Rsio.oshumCaczp ►ВCd i N L dкGm ]Aay0 cb Oo m BsX1 p N0 MpotgеsJj h ICc sNniki506Kk o Pul 123qO ax Zzb Qq O Bv dIiOj 2diрg H FfH J V mds 892% ла2dtz ng X8Rrкmonk fКы►гsrын[ X0aCCitaXi c