In computer programming, lazy evaluation is a technique that attempts to delay computation of expressions until the results of the computation are known to be needed. It has two related, yet different, meanings that could be described as delayed evaluation and minimal evaluation.
The benefits of lazy evaluation include: performance increases due to avoiding unnecessary calculations, avoiding error conditions in the evaluation of compound expressions, the ability to construct infinite data structures, and the ability to define control structures as regular functions rather than built-in primitives.
Languages that use lazy evaluation can be further subdivided into those that use a call-by-name evaluation strategy and those that use call-by-need. Most realistic lazy languages, such as Haskell, use call-by-need for performance reasons, but theoretical presentations of lazy evaluation often use call-by-name for simplicity.
The opposite of lazy evaluation is eager evaluation, also known as strict evaluation. Eager evaluation is the evaluation behavior used in most programming languages.
A corresponding approach could be taken to the evaluation of arithmetic expressions: there is no point in adding zero to a number, and multiplications by zero or one have special effects. But introducing code to test for these special values is less beneficial as a number has many values besides these special ones whereas logical variables have but two values. However, the hardware performing the calculation might well attend to special cases, if for no other reason than that zero is not representable as a normalised floating-point value.
More sensibly, suppose you have a text string with possible trailing spaces and you wish to know the location of the last non-blank character. l:=Length(t); %Pace C-programmers, where this scans the string for the terminating zero character. while l > 0 and t(l) = " " do decrement l; or, in a more flexible language, for l:=Length(t):1:-1 while t(l) = " " do; Clearly, if the string is entirely filled with spaces, l should become zero, but, there must be no attempt to access character zero of the string. It would be wasted effort at best, and an error at worst.
Some programming languages delay evaluation of expressions by default, and some others provide functions or special syntax to delay evaluation. In Miranda and Haskell, evaluation of function arguments is delayed by default. In many other languages, evaluation can be delayed by explicitly suspending the computation using special syntax (as with Scheme's "delay" and "force") or, more generally, by wrapping the expression in a thunk.
Delayed evaluation has the advantage of being able to create calculable infinite lists without infinite loops or size matters interfering in computation. For example, one could create a function that creates an infinite list (often called a stream) of Fibonacci numbers. The calculation of the n-th Fibonacci number would be merely the extraction of that element from the infinite list, forcing the evaluation of the first n members of the list only.
For example, in the Haskell programming language, the list of all Fibonacci numbers can be written as
fibs = 1 : 1 : zipWith (+) fibs (tail fibs)
In Haskell syntax, ":" prepends an element to a list, tail returns a list without its first element, and zipWith uses a specified function (in this case addition) to combine corresponding elements of two lists to produce a third.
Provided the programmer is careful, only the values that are required to produce a particular result are evaluated. However, certain calculations may result in the program attempting to evaluate an infinite number of elements; for example, requesting the length of the list or trying to sum the elements of the list with a fold operation would result in the program either failing to terminate or running out of memory.
Programming paradigms | Programming evaluation | Compiler_optimizations
Évaluation paresseuse | 느긋한 계산법 | Wartościowanie leniwe | Ленивые вычисления | 惰性计算
This article is licensed under the GNU Free Documentation License.
It uses material from the
"Lazy evaluation".
Home Page • arts • business • computers • games • health • hospitals • home • kids & teens • news • physicians • recreation• reference • regional • science • shopping • society • sports • world