I'm trying to have several initializer lists in a With
function and choose which one I want to use. So far I have this:
With[Evaluate@{Unevaluated@{a = 2, b = 3}, Unevaluated@{a = 4, b = 6}}[[1]], a + b]
which doesn't work on a pretext that "Local variable specification Unevaluated[{a=2,b=3}] is not a List.", and gives me With[Unevaluated[{a = 2, b = 3}], a + b]
for an answer. Now, if I just feed this same purportedly erroneous expression back to the kernel, it evaluates just fine with the correct result of 5, showing that Unevaluated list not being a list is a rather moot point. But how do I connect Unevaluated
with With
without me in between?
Evaluate
andUnevaluated
seem designed to confuse. $\endgroup$ – bill s Jul 1 '13 at 16:37With[{a = 2, b = 3}, a+b]
orWith[{a = 4, b = 5}, a+b]
, selecting which one I want in a single place. $\endgroup$ – panda-34 Jul 1 '13 at 16:44case[1] := {a -> 2, b -> 3}; case[2] := {a -> 4, b -> 6}; a + b /. case[2]
What do you mean by a single place? $\endgroup$ – BlacKow Jul 1 '13 at 16:50With
? $\endgroup$ – bill s Jul 1 '13 at 17:07Module
instead ofWith
?selector[n_] := Module[{}, case = {{a -> 2, b -> 3}, {a -> 4, b -> 6}}; a + b /. case[[n]]]; selector /@ {1, 2}
$\endgroup$ – BlacKow Jul 1 '13 at 17:09