Making Definitions

The replacement operator allows you to apply transformation rules to a specific expression. Often, however, you want to have transformation rules automatically applied whenever possible.

You can do this by assigning explicit values to Wolfram Language expressions and patterns. Each assignment specifies a transformation rule to be applied whenever an expression of the appropriate form occurs.

expr/.lhs->rhsapply a transformation rule to a specific expression
lhs=rhsassign a value which defines a transformation rule to be used whenever possible

Manual and automatic application of transformation rules.

This applies a transformation rule for to a specific expression.
In[1]:=
Click for copyable input
Out[1]=
By assigning a value to , you tell the Wolfram Language to apply a transformation rule for whenever possible.
In[2]:=
Click for copyable input
Out[2]=
Now is transformed automatically.
In[3]:=
Click for copyable input
Out[3]=

You should realize that except inside constructs like Module and Block, all assignments you make in a Wolfram Language session are permanent. They continue to be used for the duration of the session, unless you explicitly clear or overwrite them.

The fact that assignments are permanent means that they must be made with care. Probably the single most common mistake in using the Wolfram Language is to make an assignment for a variable like at one point in your session, and then later to use having forgotten about the assignment you made.

There are several ways to avoid this kind of mistake. First, you should avoid using assignments whenever possible, and instead use more controlled constructs such as the replacement operator. Second, you should explicitly use the deassignment operator or the function Clear to remove values you have assigned when you have finished with them.

Another important way to avoid mistakes is to think particularly carefully before assigning values to variables with common or simple names. You will often want to use a variable such as as a symbolic parameter. But if you make an assignment such as , then will be replaced by whenever it occurs, and you can no longer use as a symbolic parameter.

In general, you should be sure not to assign permanent values to any variables that you might want to use for more than one purpose. If at one point in your session you wanted the variable to stand for the speed of light, you might assign it a value such as . But then you cannot use later in your session to stand, say, for an undetermined coefficient. One way to avoid this kind of problem is to make assignments only for variables with more explicit names, such as .

x=.remove the value assigned to the object x
Clear[x,y,]clear all the values of x, y,

Removing assignments.

This does not give what you might expect, because still has the value you assigned it above.
In[4]:=
Click for copyable input
Out[4]=
This removes any value assigned to .
In[5]:=
Click for copyable input
Now this gives the result you expect.
In[6]:=
Click for copyable input
Out[6]=