Lazy evaluation

related topics
{math, number, function}
{system, computer, user}
{church, century, christian}

Strategies
Eager
Lazy
Partial
Remote
Short-circuit

In computer programming, lazy evaluation is the technique of delaying an evaluation of any expression until a value is actually being used and also avoid repeated evaluations.[1][2]

The benefits of lazy evaluation include: performance increases due to avoiding unnecessary calculations, avoiding error conditions in the evaluation of compound expressions, the capability of constructing potentially infinite data structures, and the capability of defining control structures as abstractions instead of as primitives. Lazy evaluation can lead to reduction in memory footprint, since values are created when needed.[3]

Languages that use lazy actions 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 actions is eager evaluation, sometimes known as strict evaluation. Eager evaluation is the evaluation behavior used in most programming languages.

Lazy evaluation refers to how expressions are evaluated when they are passed as arguments to functions and entails the following three points:[4]

Contents

Delayed evaluation

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. That is, a statement such as x:=expression; (i.e. the assignment of the result of an expression to a variable) clearly calls for the expression to be evaluated and the result placed in x, but what actually is in x is irrelevant until there is a need for its value via a reference to x in some later expression whose evaluation could itself be deferred, though eventually the rapidly-growing tree of dependencies would be pruned in order to produce some symbol rather than another for the outside world to see.

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" and OCaml's "lazy" and "Lazy.force") or, more generally, by wrapping the expression in a thunk. The object representing such an explicitly delayed evaluation is called a future or promise. Perl 6 uses lazy evaluation of lists, so one can assign infinite lists to variables and use them as arguments to functions, but unlike Haskell and Miranda, Perl 6 doesn't use lazy evaluation of arithmetic operators and functions by default.

Full article ▸

related documents
XSL Transformations
Queue (data structure)
B-tree
Oracle machine
Mathematica
Idempotence
Ring (mathematics)
Presburger arithmetic
Unicity distance
Preorder
Trie
Merkle-Hellman
Chain rule
Assignment problem
Richard's paradox
Extended real number line
Boolean ring
ML (programming language)
Splitting lemma
Haar measure
Definable real number
Meromorphic function
Generalized mean
Block cipher
Axiom of pairing
Monster group
Legendre symbol
Base (topology)
Mathematical model
Functional analysis