| • Science | • People | • Locations | • Timeline |
| Contents | ||
The opposite of lazy evaluation is eager evaluation, also known as strict evaluation. This is the normal evaluation behavior in most programming languages.
Minimal evaluation is an evaluation strategy in which an expression is only evaluated until the point where its final value is known. This means that in some cases it's not necessary to evaluate all the parts of an expression. Consider the following example using the C programming language:
int a = 0; if (a && myfunc(b)) { do_something(); }In this example minimal evalution guarantees that myfunc(b) is never called. This is because a evaluates to false, and false AND q evaluates to false for any value of q. This feature permits two useful programming constructs. Firstly, if the first sub-expression checks whether an expensive computation is needed and the check evaluates to false, one can eliminate expensive computation in the second argument. Secondly, it permits a construct where the first expression guarantees a condition without which the second expression may cause a runtime error, such as in the following C code where minimal evaluation prevents a null pointer dereference:
When using minimal evaluation it is important to know the expression evaluation order. A consistent order is guaranteed in some (but not all) programming languages, for example, C, Java, Perl, Python, and Ruby programming language.
It is worth noting that such expressions are simply a more compact way of saying:
if (cond_a) { if (expensive_or_dangerous_cond_b) { ... } }Minimal evaluation is also responsible for the common Perl idiom:
some_condition or die; # Abort execution if some_condition is false
Delayed evaluation is used particularly in functional languages. When using delayed evaluation, an expression is not evaluated as soon as it gets bound to a variable, but when the evaluator is forced to produce the expression's value.
Some programming languages delay evaluation of expressions by default, and some others provide functions to delay evaluation. In MirandaMiranda is a non-strict purely functional programming language developed by Professor David Turner as a successor to his earlier programming languages Sasl and KRC, using some concepts from ML and Hope. Marketed by Research Software Ltd. of England, of wh and HaskellHaskell is a standardized functional programming language with non-strict semantics, named after the logician Haskell Curry. It was created by a committee formed in the 1980s for the express purpose of defining such a language. The latest semi-official la, evaluation is delayed by default. In the Scheme programming languageThe Scheme programming language is a functional programming language and a dialect of Lisp. It was developed by Guy L. Steele and Gerald Jay Sussman in the 1970s and introduced to the academic world via a series of papers now referred to as Sussman and St, evaluation can be delayed by saying
(define delayed-expression (delay expression)).
Then (force delayed-expression) will yield the value of expression.
Delayed evaluation has the added advantage of being able to create calculable infinite lists without infinite loops or size matters interfering in computation. One could create a function that creates an infinite list of Fibonacci numbers. The calculation of the n-th Fibonacci number would be merely the extraction of that element from the infinite list. The entire infinite list is never calculated, but only the values that influence a calculation.