The two basic types of assignment in Mathematica.
|lhs = rhs ||immediate assignment: rhs is evaluated at the time of assignment |
|lhs := rhs ||delayed assignment: rhs is evaluated when the value of lhs is requested |
Assignments in Mathematica specify transformation rules for expressions. Every assignment that you make must be associated with a particular Mathematica symbol.
Assignments associated with different symbols.
|f[args] = rhs ||assignment is associated with f (downvalue) |
|t/: f[args] = rhs ||assignment is associated with t (upvalue) |
|f[g[args]] ^= rhs ||assignment is associated with g (upvalue) |
In the case of an assignment like f[args] = rhs, Mathematica looks at f, then the head of f, then the head of that, and so on, until it finds a symbol with which to associate the assignment.
When you make an assignment like lhs ^= rhs, Mathematica will set up transformation rules associated with each distinct symbol that occurs either as an argument of lhs, or as the head of an argument of lhs.
The transformation rules associated with a particular symbol s are always stored in a definite order, and are tested in that order when they are used. Each time you make an assignment, the corresponding transformation rule is inserted at the end of the list of transformation rules associated with s, except in the following cases:
• The left-hand side of the transformation rule is identical to a transformation rule that has already been stored, and any /; conditions on the right-hand side are also identical. In this case, the new transformation rule is inserted in place of the old one.
• Mathematica determines that the new transformation rule is more specific than a rule already present, and would never be used if it were placed after this rule. In this case, the new rule is placed before the old one. Note that in many cases it is not possible to determine whether one rule is more specific than another; in such cases, the new rule is always inserted at the end.