lazy evaluation

views updated

lazy evaluation An execution mechanism in which an object is evaluated only at the time when, and to the extent that, it is needed. This allows programs to manipulate objects, such as lengthy or infinite lists, whose evaluation would otherwise be needlessly time-consuming or indeed fail to terminate at all. An illustration is the problem of comparing two trees, t1 and t2, to test whether the leaves of t1, read from left to right, form the same list as the leaves of t2. The simplest solution is first to construct the two leaf-lists and then to compare them element by element. Lazy evaluation allows this program to interleave the construction of the two leaf-lists and then test for equality. The program can then terminate as soon as the lists are found to differ, without having unnecessarily constructed both lists in toto. See also strictness.