This is documentation for Mathematica 8, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)
How to | Create and Use Rules
Transformation rules in Mathematica let you set local values for symbols, functions, and all other types of expressions. Using rules provides a powerful and extensible method to replace all or part of another expression with the value you specify.
The short form for a rule uses a right arrow, which you get by typing (with no space between - and >). The Mathematica front end automatically converts into upon further typing. Either symbol is a short form for Rule.
Create the following transformation rule, which can be thought of as " goes to ":
In[1]:=
Click for copyable input
Out[1]=
By looking at the output for , you can see that this rule does not do anything: the output is simply the rule itself. This is because rules do not do anything when they are alone. You must use a rule with an expression for it to be of any use.
Rules can be applied to expressions by using (the short form for ReplaceAll). The general syntax for this is .
Use to use a rule with an expression:
In[57]:=
Click for copyable input
Out[57]=
To use two or more rules with an expression, place them in a list :
In[58]:=
Click for copyable input
Out[58]=
If you give two rules for the same variable, Mathematica will use only the first rule:
In[59]:=
Click for copyable input
Out[59]=
You can replace variables with any expression, not just individual values.
Substitute for :
In[60]:=
Click for copyable input
Out[60]=
You can also use a rule to replace larger parts of an expression:
In[62]:=
Click for copyable input
Out[62]=
In fact, you can use rules with any expression, including functions.
Substitute for :
In[65]:=
Click for copyable input
Out[65]=
Use a rule for . Note that this rule matches exactly and does not affect :
In[64]:=
Click for copyable input
Out[64]=
To replace the function regardless of its argument, you must use a pattern in the rule.
The rule can be read as " goes to ":
In[18]:=
Click for copyable input
Out[18]=
For more information on using patterns, see "Introduction to Patterns."
    
Rules that are set up using are immediate rules. That is, the right-hand side is evaluated at the same time as the rule:
In[1]:=
Click for copyable input
Out[1]=
You may need to use delayed rules instead, which are not evaluated until they are used with an expression. Delayed rules are created by using RuleDelayed.
The short form for a delayed rule is (with no space between : and >). The Mathematica front end automatically converts into upon typing. Either represents the short form for RuleDelayed:
In[2]:=
Click for copyable input
Out[2]=
Consider a problem where you want to use a rule to generate three random real numbers in the range of 0 to 1. Using an immediate rule results in the generation of the same three numbers:
In[3]:=
Click for copyable input
Out[3]=
To generate three different numbers, use a delayed rule:
In[4]:=
Click for copyable input
Out[4]=
    
Assignments set explicitly using have a global effect, while rules only affect the expression with which they are used.
Use to assign to , and then evaluate to see the value:
In[19]:=
Click for copyable input
In[2]:=
Click for copyable input
Out[2]=
Use a rule to assign a value for :
In[20]:=
Click for copyable input
Evaluating , you can see that the value assigned by the rule was not saved:
In[4]:=
Click for copyable input
Out[4]=
You must use a rule with an expression for it to work. However, you can explicitly assign a rule to a symbol and then use that symbol as you would the rule.
Use to assign the rule to , and then use with an expression:
In[68]:=
Click for copyable input
In[69]:=
Click for copyable input
Out[69]=
Since is now stored globally as the symbol , you can continue to use in place of .
Similarly, you can explicitly assign an expression to a symbol and then use a rule on the symbol:
In[22]:=
Click for copyable input
In[6]:=
Click for copyable input
Out[6]=
This is especially convenient if you plan to use the expression in more than one computation.