0
$\begingroup$

For SetDelayed, the kernel first makes the substitution literally, then does the further evaluation process if necessary.

enter image description here

I always think of SetDelayed and related functions as global rule-replacing, in contrary to the local rule-replacing, namely, ReplaceAll. However, such symmetry does not correspond so parallelly.

x + x + x + x /. x -> 3

The kernel first evaluates the left hand side expression to 4 x, then applys the substitution. So my question occurs, is there a simple build-in mechanism that literally substitutes first, then does the possible further evaluation latter? If not, what is the easy and elegant way to do it by ourselves?

$\endgroup$
4
$\begingroup$

Another way:

Unevaluated[x + x + x + x] /. x -> 3
$\endgroup$
2
$\begingroup$

A couple possibilities:

Trace @ With[{x = 3}, x + x + x + x]

{With[{x=3},x+x+x+x],3+3+3+3,12}

Trace @ ReleaseHold[Hold[x + x + x + x] /. x->3]

{{{x->3,x->3},Hold[x+x+x+x]/. x->3,Hold[3+3+3+3]},ReleaseHold[Hold[3+3+3+3]],3+3+3+3,12}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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