39 | Immediate and Delayed Values |
There are two ways to assign a value to something in the Wolfram Language:
immediate assignment (
=), and
delayed assignment (
:=).
In immediate assignment, the value is computed immediately when the assignment is done, and is never recomputed. In delayed assignment, the computation of the value is delayed, and is done every time the value is requested.
As a simple example, consider the difference between
value=RandomColor[] and
value:=RandomColor[].
In immediate assignment (
=), a random color is immediately generated:
Every time you ask for value, you get the same random color:
In delayed assignment (
:=), no random color is immediately generated:
Each time you ask for
value,
RandomColor[] is computed, and a new color is generated:
The color will typically be different every time:
It
’s very common to use
:= if something isn
’t ready yet when you
’re defining a value.
You can make a delayed assignment for circles even though n doesn’t yet have a value:
Give n a value:
Now you can ask for
circles and the value you
’ve given for
n will be used:
The idea of delayed assignment is directly analogous to the
Delayed construct we discussed for deploying webpages. In delayed assignment we don
’t compute a value until we need it. Similarly, when we use
CloudDeploy with
Delayed we don
’t compute the content of a webpage until someone asks for it.
There
’s a notion of delayed rules too.
xrhs computes
rhs immediately. But in a delayed rule
xrhs (typed
:>),
rhs is instead recomputed every time it
’s requested.
This is an immediate rule, where a specific value for
RandomReal[ ] is immediately computed:
You can replace four x’s, but they’ll all be the same:
This is a delayed rule, where the computation of
RandomReal[] is delayed:
RandomReal[] is computed separately when each
x is replaced, giving four different values:
x:=value | | delayed assignment, evaluated every time x is requested |
xvalue | | delayed rule, evaluated every time x is encountered (typed :>) |
39.1Replace
x in
{x, x+1, x+2, x^2} by the same random integer up to 100.
»
39.2Replace each
x in
{x, x+1, x+2, x^2} by a separately chosen random integer up to 100.
»
Because you don’t want to have to recompute things unless it’s necessary. It’s more efficient to just compute something once, then use the result over and over again.
How does one read
:= and
:> out loud?
:= is usually just
“colon equals
”, though sometimes
“delayed assignment
”.
:> is usually
“colon greater
”, though sometimes
“delayed rule
”.
What happens if I do
x=x+1, with
x not having a value?
You’ll start an infinite loop that’ll eventually get cut off by the system. x={x} is the same story.
What
’s the significance of inputs being labeled
In[n]:=, and outputs
Out[n]= ?
It indicates that inputs are assigned to
In[n] and outputs to
Out[n]. The
:= for input means the assignment is delayed, so that if you ask for
In[n] the result will be recomputed.
- In the Wolfram Language, the process of computing results is often called evaluation, because it involves finding values of things.
- The Wolfram Language has many ways of controlling evaluation. An example is the function Hold, which maintains an expression in “held” form until it is “released”.
- The internal form of x=y is Set[x, y]. x:=y is SetDelayed[x, y]. xy is RuleDelayed[x, y].