# How to evaluate some parts of an expression inside Hold? [duplicate]

I want to return a Hold expression but with some parts pre-evaluated. The easiest way to explain this is probably through code example. Here is what I have currently:

In  := y = 3;

In  := Hold[f[x, y]]
Out := Hold[f[x, y]]

In  := Hold[f[x, Evaluate[y]]]
Out := Hold[f[x, Evaluate[y]]]


But I want something which returns Hold[f[x, 3]]. (My actual use case is to return a Hold expression involving the value of symbols which are local to a Block.)

How can I achieve this?

• Actually it might not be a duplicate, depending on what is your starting expression. You might do With[{y=1+1}, Hold[{x,y}]], which will first evaluate 1+1 then inject the result in a held expression. Feb 17 '15 at 18:30
• Recommendation: Hold[f[x, y]] /. HoldPattern[y] :> RuleCondition[y] -- and I think this should be closed as a duplicate of the question linked above. Feb 17 '15 at 19:01
• If the items to replace are always complete Symbols you could use With as Szabolcs showed, or: Function[y, Hold[f[x, y]]][y] Feb 17 '15 at 19:03
• I came up with Function[y, Hold[f[x, y]]][y] pretty much immediately after I posted the question. But anyway I think Szabolc's With solution is slightly cleaner. Yes, the thing to replace is always a Symbol, so it's not an exact duplicate of pattern replacement problem. Feb 17 '15 at 19:41
• Since the thing to replace is always a Symbol I propose "reverse" replacement like this: y /. y_ :> Hold[f[x, y]] see: (1929) for examples. Would you object to having this question closed as "already answered" via a combination of (29317) and that? Feb 17 '15 at 19:48

In version 10 have you looked at Inactivate and Activate?

(* ClearAll["Global*"] *)
f[x_, y_] := x + y
expr = Inactivate[f[x, y]];


Now have a look at expr and see that f is inactivated. You can also activate f to see the result of the operation.

expr
Activate[expr]


Now set y and look at expr, then activate it.

y = 3;
expr
Activate[expr]
`

I hope this has helped.