# WolframLang: Set and SetDelayed

`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 .

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`

(short syntax `=`

) or
`SetDelayed`

(short syntax `:=`

)

The difference is:

`Set`

eval the right-hand-side when the rule is defined.`SetDelayed`

eval the right-hand-side at the time the replacement happens.

`Set`

is often used to set a variable.
e.g.
`x = 3`

, or set a function that remember its value.

Here's example with Fibonacci function. A function defined by f[0] is 0, f[1] is 1, and f[n] is adding its previous 2 terms. e.g. f[2] f[0] + f[1] .

(* 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 *)