Monday, July 5, 2021

Strict Evaluation vs. Lazy Evaluation, briefly

In a language that uses strict evaluation, the arguments to a function are evaluated before the function is applied.

Lazy evaluation on the other hand, delays the evaluation of an expression until it is needed.

Call by value and call by reference are examples of strict evaluation. In call by value a copy of the variable is passed, while in call by reference the variable itself is passed.

Lazy evaluation strategies are: normal order, call by name, call by macro expansion, and call by need. 

In normal order an expression is evaluated by repeatedly evaluating the left-most, outer-most reducible expression first. 

In call by name the actual parameter is only evaluated if used inside the function. If a parameter is not used in the function body, it is never evaluated. If it is used several times, it’s re-evaluated each time it appears.

Call by macro expansion is similar to call-by-name but avoids substitution. Provides developers with a mechanism to add new syntax to the core language grammar known as macros. Macros are expanded into code by a macro preprocessor. 

In call by need a parameter is evaluated only if it’s used. Once the first evaluation happens, the result is cached, so further uses of the parameter don’t require re-evaluation.

References

No comments:

Post a Comment