WolframLang: Set, SetDelayed

By Xah Lee. Date: . Last updated: .
Set[x, y]

🔸 SHORT SYNTAX: x=y

Create a transformation rule so that left-hand-side (Symbol or Pattern) becomes the right-hand-side. The right-hand-side is evaluated before the rule is defined.

The rule is associated to the left most symbol of left-hand-side expression.

(* global variable, of current session  *)
x = 5

In most programing languages, this is setting a value to a variable. In WolframLang, it's actually defining a transformation rule. The effect is the same .

Set

The left-hand-side can be any expression.

(* make f[3] to always become 5.
Effectively defining a function's value at 3.
*)
f[3] = 5

f[3]
(* 5 *)

f[4]
(* f[4] *)
(* no change *)

(* clear the rule associated with f *)
Clear[f]

The left-hand-side can be any expression or Pattern.

(* make the pattern g[x_] to always become 5. Effectively defining a constant function. *)
g[x_] = 5

(* all of the following evalutes to 5 *)
g[3]
g[4]
g[{1,2}]
g[y]

(* clear the rule associated with g *)
Clear[g]
SetDelayed[x, y]

🔸 SHORT SYNTAX: x:=y

Like Set, but right-hand-side is evaluated at the time the replacement happens.

This is usually used to define a function by way of pattern matching. [see Define Function by Pattern]

SetDelayed

Set vs SetDelayed

To create a transformation rule, use Set or SetDelayed

The difference is:

Set is often used to set a variable. (Example: x = 3) , or set a function that remember its value.

Here's example with Fibonacci function.

(* define your own fibonacci function *)
(* we use Set here, for f at 0 and 1.
because They require no computation on right-hand-side. *)
fibo[0] = 0
fibo[1] = 1

(* we use SetDelayed here,
because the right-hand-side need to be computed,
depending on the value of x. *)
fibo[x_] := fibo[x-2] + fibo[x-1]

(* fibonacci sequence from 1 to 10 *)
fiboSequence = Table[ fibo[x], {x,10}]
(* {1, 1, 2, 3, 5, 8, 13, 21, 34, 55} *)

(* check with builtin function Fibonacci *)
fiboSequence === Table[ Fibonacci[x], {x,10}]
(* True *)

Here we refine our Fibonacci function, by making it remember previously computed value. We do this by creating a new Set rule each time a new x in fibo[x] is encountered.

fibo[0] = 0
fibo[1] = 1

fibo[x_] := (fibo[x] = fibo[x-2] + fibo[x-1])
(* how this works:
We create a SetDelayed rule to transform f[x] to
(fibo[x] = fibo[x-2] + fibo[x-1]).
This creates a Set rule that transforms fibo[x] to its right-hand-side value.
The Set also returns its right-hand-side value, so
our original fibo[x_] pattern returns this value.
In the future, when fibo[x] occur with a known x, our Set rule kicks in.
The SetDelayed rule for fibo[x_] does not apply,
because the more specific rule gets applied first.
*)

(* fibonacci sequence from 1 to 10 *)
fiboSequence = Table[ fibo[x], {x,10}]
(* {1, 1, 2, 3, 5, 8, 13, 21, 34, 55} *)

(* check with builtin function Fibonacci *)
fiboSequence === Table[ Fibonacci[x], {x,10}]
(* True *)

Set is also often used to setup a sparse matrix. (a square array, where, most elements are 0, only some entries are not.)

(* a diagonal matrix *)
xMat[1,1] = 61;
xMat[2,2] = 34;
xMat[3,3] = 77;
xMat[a_,b_] = 0;

xMat[2,1]
(* 0 *)

xMat[2,2]
(* 34 *)

Pattern Matching