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.

{l1,l2,}={r1,r2,}

evaluates the ri, and assigns the results to be the values of the corresponding li.

Details

  • 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 lefthand side, but not the righthand 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 an unevaluated symbolic form, Set is treated as a scoping construct so that variables in nested occurrences are renamed if necessary. »
  • 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.

Background & Context

  • Set is a function that evaluates and assigns an expression to be the value of a variable. The expression Set[lhs,rhs] is commonly represented using the shorthand syntax lhs=rhs. After Set is evaluated, lhs is replaced by rhs whenever it appears. Depending on the form of lhs, the result is stored as in the associated OwnValues, DownValues, or a specialized data structure.
  • Set often contains patterns on the lhs that are assigned transformed values as indicated on the rhs, e.g. f[x_] = x^2. lhs=rhs evaluates rhs immediately, so in situations where rhs should be evaluated only after the assignment is made, SetDelayed (written in shorthand as :=) should be used instead.
  • The assignments associated with a given symbol can be seen using Definition[f]. Individual assignments may be removed from a symbol using Unset; Clear and ClearAll remove all definitions at once.

Examples

open allclose all

Basic Examples  (2)

Set a value for :

In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input
Out[2]=

Unset :

In[3]:=
Click for copyable input

Set multiple values:

In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input
Out[2]=

Scope  (15)

Generalizations & Extensions  (5)

Applications  (6)

Properties & Relations  (9)

Possible Issues  (4)

Neat Examples  (1)

See Also

SetDelayed  TagSet  Increment  Decrement  AddTo  TimesBy  Unset  Clear  HoldPattern  DownValues  Replace  Dynamic  Setter

Tutorials

Introduced in 1988
(1.0)