In the classification of functional languages, this is a strategy for the evaluation of expressions where the arguments to a function are evaluated only as their values are needed (which is why this is also called call by need), and then only once. This contrasts with call by value semantics where all arguments are evaluated whether or not they are actually required by the function during an application, or call by name semantics, where arguments are also evaluated as needed, but as many times as they are used. This strategy is an attempt to gain the advantages of both call by name and call by value while at the same time reducing their disadvantages.

This strategy is required for "pure" functional languages, as it is the only way referential transparency can be maintained. Arguments that might be expensive to evaluate or even might not terminate on evaluation might never be evaluated if the argument is never used. This strategy is used in languages like Haskell and Miranda which strive for functional purity. The termination properties of languages that use this strategy for evaluation are also better than those that use call by value (see the call by value node for an example).

Unfortunately, this method of evaluation is fundamentally incompatible with the presence of side effects, as an argument that has a side effect might never get evaluated in some context so the side effect doesn't happen. Functional languages that use this strategy come up with constructions called monads that attempt to get around this problem. It is also inefficient in practice when compared with call by value; it's found to not be much better than call by name. Interpreters and compilers for lazy functional languages are presently much more inefficient than their counterparts that use strict evaluation. The presence of unevaluated arguments makes the space efficiency of an algorithm in a lazy language difficult to measure.

Nevertheless, anyone who has ever had any kind of exposure to C or the C family of languages is familiar with one example of lazy evaluation. Short circuit expressions, where if we have an expression like a && b, where a has been determined to be false and b thus never evaluated, is one example of (limited) lazy evaluation.

Log in or register to write something here or to contact authors.