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.

In immediate assignment (=), a random color is immediately generated:

In[1]:= |

Out[1]= |

Every time you ask for value, you get the same random color:

In[2]:= |

Out[2]= |

In delayed assignment (:=), no random color is immediately generated:

In[3]:= |

Each time you ask for value, RandomColor[] is computed, and a new color is generated:

In[4]:= |

Out[4]= |

The color will typically be different every time:

In[5]:= |

Out[5]= |

You can make a delayed assignment for circles even though n doesn’t yet have a value:

In[6]:= |

Give n a value:

In[7]:= |

Out[7]= |

In[8]:= |

Out[8]= |

The idea of delayed assignment is directly analogous to the Delayed construct we discussed for deploying web pages. 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 web page 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:

In[9]:= |

Out[9]= |

You can replace four x’s, but they’ll all be the same:

In[10]:= |

Out[10]= |

This is a delayed rule, where the computation of RandomReal[] is delayed:

In[11]:= |

Out[11]= |

RandomReal[] is computed separately when each x is replaced, giving four different values:

In[12]:= |

Out[12]= |

Vocabulary

x:=value | delayed assignment, evaluated every time x is requested | |

xvalue | delayed rule, evaluated every time x is encountered (typed :>) |

Q&A

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.

:= is usually just “colon equals”, though sometimes “delayed assignment”. :> is usually “colon greater”, though sometimes “delayed rule”.

You’ll start an infinite loop that’ll eventually get cut off by the system. x={x} is the same story.

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.

Tech Notes

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