FACTOID # 78: 22% of New Zealanders have used cannabis.
 
 Home   Encyclopedia   Statistics   Countries A-Z   Flags   Maps   Education   Forum   FAQ   About 
 
WHAT'S NEW
RECENT ARTICLES
More Recent Articles »
 

FACTS & STATISTICS    Simple view

  1. Select countries to view: (hold down Control key and click to select several)

     

     

    Compare:

     

     

  1. Select fact or statistic: (* = graphable)

     

     

     

  2. (OPTIONAL) Compare to statistic: (both need to be graphable)

     

     

     

  3. View result as:

     

       
(OR) SEARCH ALL encyclopedia, stats & forums:   

Encyclopedia > Lazy evaluation
Programming
evaluation

Eager
Function
Lazy
Minimal
Partial
Remote
Strategy HTML and JavaScript in an IDE that uses color coding to highlight various keywords and help the developer see the function of each piece of code. ... Eager evaluation is the evaluation model in most traditional programming languages. ... An evaluation function, also known as heuristic evaluation function or static evaluation function by game-playing programs to estimate the value or goodness of a position in the minimax algorithm. ... In computer programming, lazy evaluation is a concept that attempts to minimize the work the computer has to do. ... In computing, partial evaluation is a technique for program optimization by specialization. ... In computer science, remote evaluation is a general term for any technology that involves the transmission of executable software programs from a client computer to a server computer for subsequent exection at the server. ... An evaluation strategy (or reduction strategy) for a programming language is a set of (usually deterministic) rules for defining the evaluation of expressions under β-reduction. ...

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. HTML and JavaScript in an IDE that uses color coding to highlight various keywords and help the developer see the function of each piece of code. ...


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. A binary tree, a simple type of branching linked data structure. ... In computer science and in computer programming, statements in pseudocode or in a program are normally obeyed one after the other in the order in which they are written (sequential flow of control). ...


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. An evaluation strategy (or reduction strategy) for a programming language is a set of (usually deterministic) rules for defining the evaluation of expressions under β-reduction. ... Haskell is a standardized pure functional programming language with non-strict semantics named after the logician Haskell Curry. ...


The opposite of lazy evaluation is eager evaluation, also known as strict evaluation. Eager evaluation is the evaluation behavior used in most programming languages. Eager evaluation is the evaluation model in most traditional programming languages. ... An evaluation strategy (or reduction strategy) for a programming language is a set of (usually deterministic) rules for defining the evaluation of expressions under β-reduction. ... Other listings of programming languages are: Categorical list of programming languages Generational list of programming languages Chronological list of programming languages Note: Esoteric programming languages have been moved to the separate List of esoteric programming languages. ...

Contents


Compound Expressions

Suppose a logical expression such as (a and b) is to be evaluated: should term a be false, there is no need to evaluate term b in determining the result of the expression. Likewise, in (a or b) if term a is true. These terms might be complex expressions so the gain is obvious, and leads to thought as to which terms to present first in an expression based on the expected likelihood of a result true or false as well as the cost of evaluation. Thus in an expression such as (a or b or c) one would hope that term a is both quite likely to have the value true, and be easy to evaluate. To deliver on these possibilities, the compiler would insert tests to jump out of further evaluation, giving rise to the alternative term short-circuit evaluation. These tests, should they fail to jump, will of course increase the time taken to evaluate the full expression (in the cases where that is needed) over that needed for the unconditional full-evaluation approach.


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. However, 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.


Evading error conditions

Suppose you have some expression that in certain situations must not be evaluated lest an error condition be triggered. The expression might be protected via a form such as (safetoevaluate and expression) for instance in something like (N <> 0 and (x mod N = 0)). Obviously, this could be written as if N <> 0 then if x mod N = 0 then ... which already is a little clunky. Contrariwise, you may have a case where the test is (a or b) and if a is true, not only ought b not be evaluated but doing so might or would cause trouble. Since you do wish the action taken if either a or b is true (that is what the expression means!) the polite form becomes if a then action else if b then action; which repetition conduces to mistakes.


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.


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. Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions. ...


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. In computer science, a subroutine (function, procedure, or subprogram) is a sequence of code which performs a specific task, as part of a larger program, and is grouped as one, or more, statement blocks; such code is sometimes collected into software libraries. ... Miranda is a non-strict purely functional programming language developed by Professor David Turner as a successor to his earlier programming languages SASL and KRC, using some concepts from ML and Hope. ... Scheme is a functional programming language and a dialect of Lisp. ... The term thunk is a contrived word from computer science, and has no known root. ...


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. In computing, the term stream is used in a number of ways, in all cases referring to a succession of data elements made available over time. ... In mathematics, the Fibonacci numbers form a sequence defined recursively by: In words: you start with 0 and 1, and then produce the next Fibonacci number by adding the two previous Fibonacci numbers. ...


For example, in the Haskell programming language, the list of all Fibonacci numbers can be written as Haskell is a standardized pure functional programming language with non-strict semantics named after the logician Haskell Curry. ...

 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.


See also

Compare: In computer programming, lazy evaluation is a concept that attempts to minimize the work the computer has to do. ... A strict programming language is one in which only strict functions may be defined by the user. ... Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions. ... Dataflow is a term used in computing, and may have various shades of meaning. ... The lambda calculus is a formal system designed to investigate function definition, function application, and recursion. ... Combinatory logic is a notation introduced by Moses Schönfinkel and Haskell Curry to eliminate the need for variables in mathematical logic. ... In computer science, currying is the technique of transforming a function taking multiple arguments into a function that takes a single argument (the first of the arguments to the original function) and returns a new function that takes the remainder of the arguments and returns the result. ... In computing, graph reduction avoids duplicated computation of equivalent sub-expressions of an expression. ...

In computer programming, lazy initialization is the tactic of delaying the creation of an object, the calculation of a value or some other expensive process until the first time it is needed. ... Lookahead is a tool in algorithms for looking ahead a few more input items before making a cost effective decision at one stage of the algorithm. ...

External links

  • Functional programming in Python becomes lazy

References


  Results from FactBites:
 
Lazy evaluation - Wikipedia, the free encyclopedia (981 words)
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.
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.
Lazy evaluation - definition of Lazy evaluation in Encyclopedia (564 words)
Besides performance increases, the most important benefit of lazy evaluation is that it allows one to construct an infinite data structure.
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.
As well as the formal concept of lazy evaluation in programming languages, lazy evaluation is a design pattern often seen in general computer programming.
  More results at FactBites »


 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your comments
Please enter the 5-letter protection code

Want to know more?
Search encyclopedia, statistics and forums:

 


Lesson Plans | Student Area | Student FAQ | Reviews | Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms.