8
$\begingroup$

This question already has an answer here:

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?

$\endgroup$

marked as duplicate by Alexey Popkov, Mr.Wizard Nov 8 at 15:04

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    $\begingroup$ Try using Trace to see what is happening $\endgroup$ – Jack LaVigne Aug 22 at 10:53
  • $\begingroup$ @JackLaVigne Thanks for that tip. Great diagnostic tool $\endgroup$ – Q.P. Aug 22 at 18:21
9
$\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]  *)
$\endgroup$
  • $\begingroup$ Thanks, I'll give this a whirl and get back to you! $\endgroup$ – Q.P. Aug 22 at 11:09
9
$\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}
$\endgroup$
  • $\begingroup$ Also a perfectly valid solution, very nice. $\endgroup$ – Q.P. Aug 22 at 18:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.