I'm applying the Trott-Strzebonski technique in order to replace inside a held expression.
x = Hold[1 + 1, 2 + 2, 3 + 3];
y = Hold[foo@bar[2]];
y /. bar[j_] :> With[{eval = x[[j]]}, eval /; True]
(* Hold[foo[4]] *)
However, I'd like the replacing expression to remain held during substitution:
(* Hold[foo[2 + 2]] *)
The unwanted evaluation of 2 + 2
seemed to be occurring during the Part
statement, x[[j]]
. So, I tried using Extract
instead:
y /. bar[j_] :> With[{eval = Extract[x, {j}, Unevaluated]}, eval /; True]
(* Hold[foo[Unevaluated[2 + 2]]] *)
I thought Unevaluated
would disappear as an argument of the Set
function, but this was not so. (Perhaps I still don't quite understand Unevaluated
.)
I then thought of removing that head, Unevaluated
, through another replacement, but that seems rather convoluted.
Succinctly, how does one replace parts of a held expression with held parts of another expression?