Products & Services
• Mobile Apps
• Services
Solutions
• Engineering, R&D
• Education
• Web & Software
• Finance, Statistics & Business Analysis
• Sciences
• Trends
Support & Learning
• Find an Answer
• Ask for Help
• Guided Learning
Company
• Work with Us
• Initiatives
•
BUILT-IN WOLFRAM LANGUAGE SYMBOL

# Rule

or
represents a rule that transforms lhs to rhs.

## DetailsDetails

• The character can be entered as Esc->Esc or [Rule].
• evaluates rhs immediately.
• You can apply rules using Replace.
• The assignment specifies that the rule should be used whenever it applies.
• In StandardForm, Rule is printed using .
• Symbols that occur as pattern names in lhs are treated as local to the rule. This is true when the symbols appear on the righthand side of conditions in lhs, and when the symbols appear anywhere in rhs, even inside other scoping constructs.
• Rule constructs can be nested in any way. Rule is treated as a scoping construct, so that inner variables are renamed if necessary.

## BackgroundBackground

• Rule represents a rule that transforms one expression to another. The expression Rule[lhs,rhs] is commonly written and displayed using the shorthand syntax or . Rule-based programming is an extremely powerful paradigm that allows many programs to be written both compactly and lucidly.
• A rule often contains patterns on the lhs that are replaced with appropriately transformed versions as indicated on the rhs, e.g. . Note, however, that evaluates rhs immediately, so in situations where rhs should be evaluated only after the rule is used, RuleDelayed (written in shorthand as or ) should be used instead.
• Rule replacement can be performed using functions like Replace, ReplaceAll, ReplaceRepeated, ReplaceList, ReplacePart, and StringReplace.
• Many functions in the Wolfram Language return their results as lists of rules, including Solve, FindInstance, and FindRoot, e.g. Solve[x^2-1==0,x] returns . This form is useful since it both keeps solutions associated with their respective variables (especially in the multivariate case) and allows convenient backsubstitution of the solutions in terms of the original variables via ReplaceAll or related functions.

## ExamplesExamplesopen allclose all

### Basic Examples  (1)Basic Examples  (1)

Use a rule that replaces by 3:

 Out[1]=