2.5.8 Immediate and Delayed Definitions
You may have noticed that there are two different ways to make assignments in Mathematica: lhs = rhs and lhs := rhs. The basic difference between these forms is when the expression rhs is evaluated. lhs = rhs is an immediate assignment, in which rhs is evaluated at the time when the assignment is made. lhs := rhs, on the other hand, is a delayed assignment, in which rhs is not evaluated when the assignment is made, but is instead evaluated each time the value of lhs is requested.
The two types of assignments in Mathematica.
This uses the := operator to define the function ex.
In[1]:= ex[x_] := Expand[(1 + x)^2]
Because := was used, the definition is maintained in an unevaluated form.
In[2]:= ?ex
When you make an assignment with the = operator, the righthand side is evaluated immediately.
In[3]:= iex[x_] = Expand[(1 + x)^2]
Out[3]=
The definition now stored is the result of the Expand command.
In[4]:= ?iex
When you execute ex, the Expand is performed.
In[5]:= ex[y + 2]
Out[5]=
iex simply substitutes its argument into the already expanded form, giving a different answer.
In[6]:= iex[y + 2]
Out[6]=
As you can see from the example above, both = and := can be useful in defining functions, but they have different meanings, and you must be careful about which one to use in a particular case.
One rule of thumb is the following. If you think of an assignment as giving the final "value" of an expression, use the = operator. If instead you think of the assignment as specifying a "command" for finding the value, use the := operator. If in doubt, it is usually better to use the := operator than the = one.
Interpretations of assignments with the = and := operators.
Although := is probably used more often than = in defining functions, there is one important case in which you must use = to define a function. If you do a calculation, and get an answer in terms of a symbolic parameter x, you often want to go on and find results for various specific values of x. One way to do this is to use the /. operator to apply appropriate rules for x in each case. It is usually more convenient however, to use = to define a function whose argument is x.
Here is an expression involving x.
In[7]:= D[Log[Sin[x]]^2, x]
Out[7]=
This defines a function whose argument is the value to be taken for x.
In[8]:= dlog[x_] = %
Out[8]=
Here is the result when x is taken to be 1 + a.
In[9]:= dlog[1 + a]
Out[9]=
An important point to notice in the example above is that there is nothing special about the name x that appears in the x_ pattern. It is just a symbol, indistinguishable from an x that appears in any other expression.
Defining functions for evaluating expressions.
You can use = and := not only to define functions, but also to assign values to variables. If you type x = value, then value is immediately evaluated, and the result is assigned to x. On the other hand, if you type x := value, then value is not immediately evaluated. Instead, it is maintained in an unevaluated form, and is evaluated afresh each time x is used.
This evaluates Random[ ] to find a pseudorandom number, then assigns this number to r1.
In[10]:= r1 = Random[ ]
Out[10]=
Here Random[ ] is maintained in an unevaluated form, to be evaluated afresh each time r2 is used.
In[11]:= r2 := Random[ ]
Here are values for r1 and r2.
In[12]:= {r1, r2}
Out[12]=
The value of r1 never changes. Every time r2 is used, however, a new pseudorandom number is generated.
In[13]:= {r1, r2}
Out[13]=
The distinction between immediate and delayed assignments is particularly important when you set up chains of assignments.
This defines a to be 1.
In[14]:= a = 1
Out[14]=
Here a + 2 is evaluated to give 3, and the result is assigned to be the value of ri.
In[15]:= ri = a + 2
Out[15]=
Here a + 2 is maintained in an unevaluated form, to be evaluated every time the value of rd is requested.
In[16]:= rd := a + 2
In this case, ri and rd give the same values.
In[17]:= {ri, rd}
Out[17]=
Now the value of a is changed.
In[18]:= a = 2
Out[18]=
Now rd uses the new value for a, while ri keeps its original value.
In[19]:= {ri, rd}
Out[19]=
You can use delayed assignments such as t := rhs to set up variables whose values you can find in a variety of different "environments". Every time you ask for t, the expression rhs is evaluated using the current values of the objects on which it depends.
The righthand side of the delayed assignment is maintained in an unevaluated form.
In[20]:= t := {a, Factor[x^a  1]}
This sets a to 4, then finds the value of t.
In[21]:= a = 4; t
Out[21]=
Here a is 6.
In[22]:= a = 6; t
Out[22]=
In the example above, the symbol a acts as a "global variable", whose value affects the value of t. When you have a large number of parameters, many of which change only occasionally, you may find this kind of setup convenient. However, you should realize that implicit or hidden dependence of one variable on others can often become quite confusing. When possible, you should make all dependencies explicit, by defining functions which take all necessary parameters as arguments.
Two types of transformation rules in Mathematica.
Just as you can make immediate and delayed assignments in Mathematica, so you can also set up immediate and delayed transformation rules.
The righthand side of this rule is evaluated when you give the rule.
In[23]:= f[x_] > Expand[(1 + x)^2]
Out[23]=
A rule like this is probably not particularly useful.
In[24]:= f[x_] > Expand[x]
Out[24]=
Here the righthand side of the rule is maintained in an unevaluated form, to be evaluated every time the rule is used.
In[25]:= f[x_] :> Expand[x]
Out[25]=
Applying the rule causes the expansion to be done.
In[26]:= f[(1 + p)^2] /. f[x_] :> Expand[x]
Out[26]=
In analogy with assignments, you should typically use > when you want to replace an expression with a definite value, and you should use :> when you want to give a command for finding the value.
