2
$\begingroup$

Consider an expression of type

f[a,b]+a+b

and a set of replacement rules

sub={a->1,b->2}

If I evaluate now the expression

f[a,b]+a+b/.sub

the output will be

3+f[1,2]

However, I need that the arguments of f are left unevaluated after the replacement rules are applied. In particular, I need my output to be

3+f[a,b]

I tried with HoldAllComplete and similars as attributes to f, but this seems not to work

$\endgroup$

1 Answer 1

3
$\begingroup$

You can change your rule slightly to reflect the fact that you want to leave the f expression unchanged:

f[a, b] + a + b /. {expr : f[__] :> expr, a -> 1, b -> 2}

3 + f[a, b]

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.