39 | Immediate and Delayed Values |
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: 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: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. x rhs computes rhs immediately. But in a delayed rule x rhs (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 |
x value | delayed rule, evaluated every time x is encountered (typed :>) |
Answer & check your solution
39.2 Replace each x inAnswer & check your solution
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 ” .
You ’ ll start an infinite loop that ’ ll eventually get cut off by the system. x=
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.