Wolfram Research

Function Repository Resource:

RecursiveRewrite

Source Notebook

Rewrite an expression replacing repeatedly by strings

Contributed by: Enrique Zeleny

ResourceFunction["RecursiveRewrite"][expr]

rewrites expr replacing repeatedly by strings.

Details and Options

Any string can be used in the RewriteSymbol option.

Examples

Basic Examples

Choose an expression:

In[1]:=
expr = (#1[[1, 2]] &) /@ Solve[8 x^4 + 42 x^3 + 64 x^2 + 71 x + 27 == 0, x, Quartics -> True];

Get a list of replacements to rewrite the expression:

In[2]:=
optexpr = ResourceFunction["RecursiveRewrite"][expr]
Out[2]=

Recover the original expression, applying replacements only six times:

In[3]:=
expr2 = Nest[# /. optexpr &, "c58", 6]
Out[3]=

To see the values of subexpressions, you can use the function FromRecursiveRewrite instead:

In[4]:=
ResourceFunction["FromRecursiveRewrite"][expr2[[1]], optexpr, Tooltip[Framed[#1, Background -> GrayLevel[.95]], #2] &]
Out[4]=

Options

RewriteSymbol

In[5]:=
optexpr2 = ResourceFunction["RecursiveRewrite"][expr[[1, 2]], RewriteSymbol -> "R"]
Out[5]=

Recover the original expression and rewrite terms:

In[6]:=
Nest[# /. optexpr2 &, "R26", 6] /. s_String :> Subscript[R, StringDelete[s, "R"]]
Out[6]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

License Information