Replace¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
Replace[expr, rules]
tries to match expr at the top level against rules and returns the rewritten form; if no rule matches, returns expr unchanged.
Replace[expr, rules, levelspec]
applies rules only at the parts of expr specified by levelspec.
Notes
Matching tries each rule in order and uses the first that succeeds; rules may be a single rule or a list of rules.Examples (10)¶
Every input below was run against the current Mathilda build and its output recorded.
Basic examples (4)¶
In[1]:= Replace[x^2, x^2 -> a + b]
Out[1]= a + b
In[2]:= Replace[1 + x^2, x^2 -> a + b]
Out[2]= 1 + x^2
In[3]:= Replace[x, {{x -> a}, {x -> b}}]
Out[3]= {a, b}
In[4]:= Replace[1 + x^2, x^2 -> a + b, {1}]
Out[4]= 1 + a + b
Applications (6)¶
In[5]:= Replace[x^2, x^2 -> done]
Out[5]= done
In[6]:= Replace[x, {x -> 1, _ -> 0}]
Out[6]= 1
In[7]:= Replace[w, {x -> 1, _ -> 0}]
Out[7]= 0
In[8]:= Replace[a + b + c + d, x_ + y_ -> {x, y}]
Out[8]= {a, b + c + d}
In[9]:= Replace[{{1, 2}, {3, 4}}, x_Integer :> x^2, {2}]
Out[9]= {{1, 4}, {9, 16}}
In[10]:= Replace[{1, {2, {3, {4}}}}, x_Integer :> x^2, {-1}]
Out[10]= {1, {4, {9, {16}}}}
Implementation notes¶
Algorithm. builtin_replace (src/replace.c) differs from ReplaceAll in that it applies rules only at the levels named by an optional level-spec (third argument). The handler parses the level-spec into [min_l, max_l] (an integer n → levels 1..n; {n} → exactly n; {m,n} → m..n; All/Infinity map to wide bounds; negative bounds select by depth-from-bottom), plus a Heads -> True|False option. It then calls apply_replace_nested → do_replace_at_level, a bottom-up traversal: it first recurses into the head (only if heads) and all arguments, rebuilds the node, and then, if the current node's level lies in [min_l, max_l] (level computed against get_expr_depth_replace for negative specs), tries each ReplaceRule in order via match/replace_bindings and returns the first replacement. Because matching happens after the children are already transformed and a match short-circuits further rule attempts at that node, Replace rewrites each in-range position once. With no level-spec it defaults to level 0 only (the whole expression).
Data structures. ReplaceRule[] of borrowed pattern/replacement pointers; MatchEnv per attempt. A list of non-rule sub-lists is threaded into parallel results, as in ReplaceAll.
Protected.- Rules must be of the form
lhs -> rhs(Rule) orlhs :> rhs(RuleDelayed). - Tries rules in order. The first one that matches is applied.
- If rules are given in nested lists,
Replaceis mapped onto the inner lists. - Standard level specifications (
n,Infinity,All,{n},{n1, n2}) are fully supported with the default being{0}(the whole expression). - Expressions at deeper levels in a subexpression are matched first.
- Replaces parts even inside
Holdor related wrappers.
Attributes: Protected.
References¶
See also: Rule, RuleDelayed, Hold
- Source:
src/replace.c - Specification:
docs/spec/builtins/assignment-and-rules.md - Tests:
tests/test_assoc_atomicity.c - Tests:
tests/test_assoc_forms.c - Tests:
tests/test_association.c - Tests:
tests/test_condition_downvalue.c
Notes & additional examples¶
Notes¶
Replace[expr, rules] matches expr only at the top level (unlike
ReplaceAll, which descends into every subexpression). The first matching
rule in the list wins; a fall-through _ -> 0 rule acts as a default. A
levelspec third argument restricts where matching happens: {2} targets
parts two levels deep, and the negative spec {-1} targets the leaves, so
x_Integer :> x^2 squares every atom in a nested list. Because matching
respects flat/orderless heads, x_ + y_ against a four-term sum binds x to
the first term and y to the rest.