Advertisement

Lazy evaluation

From Academic Kids

In computer programming, lazy evaluation is a concept that attempts to minimize the work the computer has to do. It has two related, yet different, meanings that could be described as delayed evaluation and minimal evaluation. Besides performance increases, the most important benefit of lazy evaluation is that it allows one to construct an infinite data structure.

The opposite of lazy evaluation is eager evaluation, also known as strict evaluation. This is the normal evaluation behavior in most programming languages.

Contents

Minimal evaluation

Minimal evaluation (also known as short circuit evaluation) is an evaluation strategy in which an expression is only evaluated until the point where its final value is known. This means that in some cases it's not necessary to evaluate all the parts of an expression. Consider the following example using the C programming language:

   int a = 0;
   if (a && myfunc(b)) {
       do_something();
   }

In this example minimal evalution guarantees that myfunc(b) is never called. This is because a evaluates to false, and false AND q evaluates to false for any value of q. This feature permits two useful programming constructs. Firstly, if the first sub-expression checks whether an expensive computation is needed and the check evaluates to false, one can eliminate expensive computation in the second argument. Secondly, it permits a construct where the first expression guarantees a condition without which the second expression may cause a runtime error, such as in the following C code where minimal evaluation prevents a null pointer dereference:

   bool is_three_chars_long(const char *p) {
     return p && (strlen(p) == 3);
   }

When using minimal evaluation it is important to know the expression evaluation order. A consistent order is guaranteed in some (but not all) programming languages, for example, C, Java, Perl, Python, and Ruby.

It is worth noting that such expressions are simply a more compact way of saying:

   if (cond_a) {
       if (expensive_or_dangerous_cond_b) {
           ...
       }
   }

Minimal evaluation is also responsible for the common Perl idiom:

 some_condition or die;    # Abort execution if some_condition is false

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.

Some programming languages delay evaluation of expressions by default, and some others provide functions to delay evaluation. In Miranda and Haskell, evaluation is delayed by default. In the Scheme programming language, evaluation can be delayed by saying (define delayed-expression (delay expression)). Then (force delayed-expression) will yield the value of expression.

Delayed evaluation has the added advantage of being able to create calculable infinite lists without infinite loops or size matters interfering in computation. One could create a function that creates an infinite list 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)

Here, : 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, the entire infinite list is never calculated, but only the values that are required to produce a result. However, certain calculations may result in the program attempting to evaluate the entire list and crashing; for example requesting the length of the list, or trying to sum the elements of the list with a fold operation.

Lazy evaluation as a design pattern

As well as the formal concept of lazy evaluation in programming languages, lazy evaluation is a design pattern often seen in general computer programming.

For example, in modern computer window managers, the painting of information to the screen is driven by "expose events" which drive the display code at the last possible moment. By doing this, they avoid the over-eager computation of unnecessary display content.

Another example of laziness in modern computer systems is copy-on-write page allocation.

See also

Compare:

Tutorials

Navigation

Academic Kids Menu

  • Art and Cultures
    • Art (http://www.academickids.com/encyclopedia/index.php/Art)
    • Architecture (http://www.academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (http://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (http://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools