Wolfram Research

Function Repository Resource:

ReplaceAllUnheld

Source Notebook

Apply replacement rules to evaluated parts of expression

Contributed by: Wolfram Research

ResourceFunction["ReplaceAllUnheld"][expr,rules]

applies a rule or list of rules in an attempt to transform each subpart of expr that would be automatically evaluated.

Details and Options

ReplaceAll operates on all subparts of an expression; ResourceFunction["ReplaceAllUnheld"] operates only on those subparts that would normally be evaluated.

Examples

Basic Examples

In[1]:=
ResourceFunction["ReplaceAllUnheld"][If[a, a, a], a -> b]
Out[1]=

Scope

Functions that hold some of their arguments:

In[2]:=
SetAttributes[hf, HoldFirst]
In[3]:=
SetAttributes[hr, HoldRest]
In[4]:=
ResourceFunction["ReplaceAllUnheld"][{hf[x, x], hr[x, x]}, x -> 1]
Out[4]=

Explicitly held expressions:

In[5]:=
ResourceFunction["ReplaceAllUnheld"][{x, Hold[x], Unevaluated[x]}, x -> 1]
Out[5]=

Properties and Relations

ReplaceAll applies the replacement rules to all arguments, whether held or evaluated:

In[6]:=
ReplaceAll[If[x, x, x], x -> 1]
Out[6]=

Possible Issues

No replacement happens, since ReplaceAllUnheld does not itself have have any Hold attributes:

In[7]:=
a := Random[]
In[8]:=
ResourceFunction["ReplaceAllUnheld"][If[a, a, a], a -> b]
Out[8]=
In[9]:=
Attributes[
ResourceFunction["ReplaceAllUnheld"]]
Out[9]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

License Information