ArticleslgStudy

mathematics

Lazy evaluation

Lazy evaluation is a mathematics topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Lazy evaluation rather than just read about it. In short: In programming language theory, lazy evaluation is an evaluation strategy that delays the evaluation of an expression until its value is needed (non-strict evaluation). In some contexts, the term refers specifically to call-by-need, which additionally avoids repeated evaluations (by the use of sharing).

Key takeaways

  • Lazy evaluation belongs to mathematics; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Lazy evaluation to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Lazy evaluation from memory before moving on to harder problems.

Reference excerpt

In programming language theory, lazy evaluation is an evaluation strategy that delays the evaluation of an expression until its value is needed (non-strict evaluation). In some contexts, the term refers specifically to call-by-need, which additionally avoids repeated evaluations (by the use of sharing). The benefits of lazy evaluation include:

The ability to define control flow (structures) as abstractions instead of primitives. The ability to define potentially infinite data structures. This allows for more straightforward implementation of some algorithms. The ability to define partly defined data structures where some elements are errors. This allows for rapid prototyping. Lazy evaluation is often combined with memoization, as described in Jon Bentley's Writing Efficient Programs. After a function's value is computed for that parameter or set of parameters, the result is stored in a lookup table that is indexed by the values of those parameters; the next time the function is called, the table is consulted to determine whether the result for that combination of parameter values is already available. If so, the stored result is simply returned. If not, the function is evaluated, and another entry is added to the lookup table for reuse. Lazy evaluation is difficult to combine with imperative features such as exception handling and input/output, because the order of operations becomes indeterminate. The opposite of lazy evaluation is eager evaluation, sometimes known as strict evaluation. Eager evaluation is the evaluation strategy employed in most programming languages.

History Lazy evaluation was introduced for lambda calculus by Christopher Wadsworth. For programming languages, it was independently introduced by Peter Henderson and James H. Morris and by Daniel P. Friedman and David S. Wise.

Applications Delayed evaluation is used particularly in functional programming 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 is 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 to produce some symbol rather than another for the outside world to see. Lazy evaluation is fundamental in big data frameworks such as Apache Spark, where computations on distributed datasets are delayed until results are explicitly needed, allowing for execution optimizations and reduction of unnecessary processing.

Control structures

Lazy evaluation allows control structures to be defined normally, and not as primitives or compile-time techniques. For example, one can define if-then-else and short-circuit evaluation operators:

These have the usual semantics, i.e., ifThenElse a b c evaluates (a), then if and only if (a) evaluates to true does it evaluate (b), otherwise it evaluates (c). That is, exactly one of (b) or (c) will be evaluated. Similarly, for EasilyComputed || LotsOfWork, if the easy part gives True the lots of work expression could be avoided. Finally, when evaluating SafeToTry && Expression, if SafeToTry is false there will be no attempt at evaluating the Expression. Conversely, in an eager language the above definition for ifThenElse a b c would evaluate (a), (b), and (c) regardless of the value of (a). This is not the desired behavior, as (b) or (c) may have side effects, take a long time to compute, or throw errors. It is usually possible to introduce user-defined lazy control structures in eager languages as functions, though they may depart from the language's syntax for eager evaluation: Often the involved code bodies need to be wrapped in a function value, so that they are executed only when called.

Working with infinite data structures Delayed evaluation has the advantage of being able to create calculable infinite lists without infinite loops or size matters interfering in computation. The actual values are only computed when needed. 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 only the first n members of the list. Take for example this trivial program in Haskell:

In the function numberFromInfiniteList, the value of infinity is an infinite range, but until an actual value (or more specifically, a specific value at a certain index) is needed, the list is not evaluated, and even then, it is only evaluated as needed (i.e., only to the desired index). Provided the programmer is careful, the program completes normally. 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. As another example, the list of all Fibonacci numbers can be written in the programming language Haskell as:

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.

List-of-successes pattern

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Lazy evaluation

Start with the simplest possible case. Write down what Lazy evaluation claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In mathematics, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Lazy evaluation before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Lazy evaluation ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Lazy evaluation

In research
Lazy evaluation appears in mathematics research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Lazy evaluation in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Lazy evaluation is common in secondary-school and first-year university syllabi. It links to neighbouring topics Compiler optimizations, Evaluation strategy, Implementation of functional programming languages, so understanding it makes those chapters shorter.
In everyday life
Look for Lazy evaluation outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Lazy evaluation” →

How to study Lazy evaluation in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Lazy evaluation means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Lazy evaluation out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Lazy evaluation in simple terms?

In programming language theory, lazy evaluation is an evaluation strategy that delays the evaluation of an expression until its value is needed (non-strict evaluation). In some contexts, the term refers specifically to call-by-need, which additionally avoids repeated evaluations (by the use of shar…

Why does Lazy evaluation matter?

Because it connects several mathematics ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Lazy evaluation?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Lazy evaluation.

Tags

  • Compiler optimizations
  • Evaluation strategy
  • Implementation of functional programming languages
  • Programming language comparisons

Keep exploring