Lazy Evaluation

Lazy evaluation referred to in the computer science a kind of evaluation of expressions, in which the result of the evaluated expression is calculated only as far as it is needed.

One advantage of such an evaluation strategy is time saving, as function calls can be at least partially saved or avoided altogether. Also lazy evaluation gives the programmer the ability to use unlimited data structures. A disadvantage is the complicated error cleanup in programs that are lazy evaluated. Often it is not comprehensible at first glance whether an expression has been evaluated at a given time. This is particularly problematic when function calls can have a side effect.

A restricted logical expressions special case is the short-circuit evaluation, which is implemented in many non -lazy - language such as C or Java.

Example

The following is written in Haskell example shows an application of lazy evaluation.

Squares, n = (n * n ) squares (n 1) The function calculates the squares infinite list of all square numbers beginning with n The square of 3 therefore could be given by the expression 0 squares! 3 directions - it obsolete l! x is the element at position x from the list l Without lazy evaluation would not terminate the call of squares 0. Instead, only the parts are calculated, which are really needed. The internal evaluation looks shortened as follows:

Squares 0! 3 → 0 * 0: squares (0 1)! 3 → squares (0 1)! 2 → (0 1) * (0 1): squares (0 1 1)! 2 ... → (0 1 1 1 ) * ( 0 1 1 1): squares (0 1 1 1 1)! 0 → (0 1 1 1 ) * ( 0 1 1 1) → 9

At first glance, the lazy evaluation seems here to perform multiple computations as to the right of the squares function of the parameter n is used several times, and, instead of a value of a non- performed calculation is used. Since Haskell is a purely functional language that ensures referential transparency that an expression always has the same result. Thus, each expression only once must be calculated. This utilizes Haskell by internally n on the right side of the function definition in place of, for example, (0 1 1 1 ), a pointer is used for the calculation. When an expression at a point in the program calculated, the result is also available at other locations available.

  • Programming paradigm
5812
de