There is a function ReplaceAllUnheld metioned in the doc Developer/ref/ReplaceAllUnheld.

I am curious about what is the practical usage of this function? The basic example in the doc is not interesting. Has anyone used this function to do something?


1 Answer 1


Thank you for bringing this function to my attention! It lets us use ReplaceAll without disturbing or even traversing held expressions. This is huge! I have little time at the moment and I intend to flesh out this answer later but I want to get some ink on the page lest I forget.

Many times Hold or HoldForm is used as a container to prevent automatic alteration of an expression (via evaluation) but as replacements traditionally operate independently of evaluation (which itself is extremely useful of course) one has to be particularly careful in using replacements for generation expression manipulation. With ReplaceAllUnheld we can make these replacements without contaminating our containers. Previously one might use a skip rule(1)(2)(3) to pass over explicit appearances of the Hold family:

skipHolds = skip : _Hold | _HoldForm | _HoldComplete | _HoldPattern :> skip;

{HoldForm[a^2], a^2} /. {skipHolds, a -> 5}
{a^2, 25}

This however does nothing for user containers or even system functions with HoldFirst, HoldRest etc. The Developer function lets us seamlessly handle both.

As a quickly contrived example (where also the skip rule would work) imagine we build a table like this:

foo = {a^Range[3], b^Range[3]};
table = TableForm[KroneckerProduct @@ foo, TableHeadings -> Map[HoldForm, foo, {2}]]

enter image description here

Later we would like to substitute values into the body of the table without affecting the headings. That can now be done with:

Developer`ReplaceAllUnheld[table, {a -> 3, b -> 7}]

enter image description here

This only touches on the potential utility of this function and again I hope to both explore and write more about it later.

  • $\begingroup$ Thank you so much for the explanation, Mr. Wizard! Accepted! looking forward to more illustrations : ) $\endgroup$
    – matheorem
    Commented Dec 25, 2016 at 7:08
  • $\begingroup$ This is interesting! Any further information you could share, as you teased, would be welcome. $\endgroup$
    – QuantumDot
    Commented Jun 17, 2018 at 18:35
  • $\begingroup$ @QuantumDot I didn't mean it as a tease. I have not been using Mathematica much recently, and I don't believe I ever made personal use of ReplaceAllUnheld. I still think it is an interesting function with significant potential, even if I never explored this as I previously intended. $\endgroup$
    – Mr.Wizard
    Commented Jun 23, 2018 at 8:09

Your Answer

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

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