Using `With[…]` with a list specification as a variableDp j 8 Zls T67wtnja MMy89Arrg Rr Zzk La50Ns3P
4
$\\begingroup$
$\\endgroup$
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?
evaluation scoping expression-construction
-
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
add a comment |
2 Answers
active
oldest
votes
6
$\\begingroup$
$\\endgroup$
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] *)
-
$\\begingroup$ Thanks, I'll give this a whirl and get back to you! $\\endgroup$ – QuantumPenguin 8 hours ago
add a comment |
4
$\\begingroup$
$\\endgroup$
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}
-
$\\begingroup$ Also a perfectly valid solution, very nice. $\\endgroup$ – QuantumPenguin 1 hour ago
add a comment |