This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)
 Documentation / Mathematica / Built-in Functions / Programming / Assignments  /
Set

  • lhs = rhs evaluates rhs and assigns the result to be the value of lhs. From then on, lhs is replaced by rhs whenever it appears.
  • , , ... = , , ... evaluates the , and assigns the results to be the values of the corresponding .
  • lhs can be any expression, including a pattern.
  • f[x_] = x^2 is a typical assignment for a pattern. Notice the presence of _ on the left-hand side, but not the right-hand side.
  • An assignment of the form f [ args ] = rhs sets up a transformation rule associated with the symbol f.
  • Different rules associated with a particular symbol are usually placed in the order that you give them. If a new rule that you give is determined to be more specific than existing rules, it is, however, placed before them. When the rules are used, they are tested in order.
  • New assignments with identical lhs overwrite old ones.
  • You can see all the assignments associated with a symbol f using ? f or Definition[ f ].
  • If you make assignments for functions that have attributes like Flat and Orderless, you must make sure to set these attributes before you make assignments for the functions.
  • Set has attribute HoldFirst.
  • If lhs is of the form f [ args ], then args are evaluated.
  • There are some special functions for which an assignment to s [ f [ args ]] is automatically associated with f rather than s. These functions include: Attributes, Default, Format, MessageName, Messages, N and Options.
  • When it appears in symbolic form, Set is treated as a scoping construct (see Section A.3.8).
  • lhs = rhs returns rhs even if for some reason the assignment specified cannot be performed.
  • Some global variables such as $RecursionLimit can only be assigned a certain range or class of values.
  • See the Mathematica book: Section 1.4.2, Section 2.4.5, Section 2.4.8Section A.5.2.
  • See also: TagSet, Unset, Clear, HoldPattern, DownValues.

    Further Examples

    When you make an assignment with the Set (or =) operator, the right-hand side is evaluated immediately.

    In[1]:=

    Out[1]=

    on the other hand, with SetDelayed the evaluation is postponed until zzz is evaluated, so no output is generated.

    In[2]:=

    The difference between yyy and zzz is not apparent until x is set to a symbolic expression.

    In[3]:=

    Out[3]=

    In[4]:=

    Out[4]=

    In[5]:=

    Out[5]=

    In[6]:=

    Out[6]=

    In[7]:=

    Out[7]=

    In[8]:=