Lazy evaluation
This concept is very simple. By default, F# follows the eager evaluation (https://en.wikipedia.org/wiki/Eager_evaluation) strategy, or an expression is evaluated as soon as it is bound. The alternative strategy available in other functional programming languages is to postpone the calculations until their result is absolutely necessary. F# can be explicitly told where to use lazy evaluation; by default, it uses lazy evaluations only for sequences. Expressing lazy evaluation if F# is not complicated syntactically, the following binding serves the purpose as shown:
let name = lazy ( expression )
Here, name
is bound to the result of calculating expression
, but the calculation itself is postponed. The type of value name
is a special one, that is, Lazy<'T>;
it represents a wrapper over 'T
, which is the type of the expression per se. The computation gets performed by calling the Force
method of type Lazy<'T>
, like this name.Force()
. This action also unwraps the underlying...