The concept of transformation rules for arbitrary symbolic patterns is key in Mathics3.
Also, functions can get applied or transformed depending on whether or not functions arguments match.
Some examples:
a + b + c /. a + b -> t
a + 2 + b + c + x * y /. n_Integer + s__Symbol + rest_ -> {n, s, rest}
f[a, b, c, d] /. f[first_, rest___] -> {first, {rest}}
Tests and Conditions:
f[4] /. f[x_?(# > 0&)] -> x ^ 2
f[4] /. f[x_] /; x > 0 -> x ^ 2
Elements in the beginning of a pattern rather match fewer elements:
f[a, b, c, d] /. f[start__, end__] -> {{start}, {end}}
Optional arguments using Optional
:
f[a] /. f[x_, y_:3] -> {x, y}
Options using OptionsPattern
and OptionValue
:
f[y, a->3] /. f[x_, OptionsPattern[{a->2, b->5}]] -> {x, OptionValue[a], OptionValue[b]}
The attributes Flat
, Orderless
, and OneIdentity
affect pattern matching.