8

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?

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
user27119
  • 2,500
  • 13
  • 34

2 Answers2

9

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]  *)
Michael E2
  • 235,386
  • 17
  • 334
  • 747
9

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}
Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368