Rule

lhs->rhs or lhsrhs

represents a rule that transforms lhs to rhs.

Details

  • Rules are very general constructs that can represent transformation, subcasing, correspondence and other relations between expressions.
  • The character can be entered as -> or [Rule].
  • lhs->rhs evaluates rhs immediately.
  • You can apply rules using Replace.
  • The assignment lhs=rhs specifies that the rule lhs->rhs 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.

Background & Context

  • Rule represents a rule that transforms one expression to another. The expression Rule[lhs,rhs] is commonly written and displayed using the shorthand syntax lhs->rhs or lhs->rhs. 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. x_->x^2. Note, however, that lhs->rhs 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 {{x-1},{x1}}. 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.

Examples

open allclose all

Basic Examples  (1)

Use a rule that replaces x by 3:

Scope  (1)

Any expression or pattern can appear in a rule:

Properties & Relations  (4)

-> evaluates when it is first entered; :> when it is used:

-> groups to the right:

KeyValuePattern can be used to transform a list of rules:

KeyValuePattern distinguishes between Rule and RuleDelayed:

Wolfram Research (1988), Rule, Wolfram Language function, https://reference.wolfram.com/language/ref/Rule.html (updated 1996).

Text

Wolfram Research (1988), Rule, Wolfram Language function, https://reference.wolfram.com/language/ref/Rule.html (updated 1996).

CMS

Wolfram Language. 1988. "Rule." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 1996. https://reference.wolfram.com/language/ref/Rule.html.

APA

Wolfram Language. (1988). Rule. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Rule.html

BibTeX

@misc{reference.wolfram_2024_rule, author="Wolfram Research", title="{Rule}", year="1996", howpublished="\url{https://reference.wolfram.com/language/ref/Rule.html}", note=[Accessed: 18-November-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_rule, organization={Wolfram Research}, title={Rule}, year={1996}, url={https://reference.wolfram.com/language/ref/Rule.html}, note=[Accessed: 18-November-2024 ]}