Eager evaluation
Encyclopedia
In computer programming
, eager evaluation or greedy evaluation is the evaluation strategy
in most traditional programming language
s. In eager evaluation an expression
is evaluated as soon as it gets bound to a variable
. The term is typically used to contrast lazy evaluation
, where expressions are only evaluated when evaluating a dependent expression. Eager evaluation is almost exclusively used in imperative
programming languages where the order of execution is implicitly defined by the source code organization.
One advantage of eager evaluation is that it eliminates the need to track and schedule the evaluation of expressions. It also allows the programmer to dictate the order of execution, making it easier to determine when sub-expressions (including functions) within the expression will be evaluated, as these sub-expressions may have side-effects that will affect the evaluation of other expressions.
A disadvantage of eager evaluation is that it forces the evaluation of expressions that may not be necessary at run time, or it may delay the evaluation of expressions that have a more immediate need. It also forces the programmer to organize the source code for optimal order of execution.
Note that many modern compilers are capable of scheduling execution to better optimize processor resources and can often eliminate unnecessary expressions from being executed entirely. Therefore, the notions of purely eager or purely lazy evaluation may not be applicable in practice.
Computer programming
Computer programming is the process of designing, writing, testing, debugging, and maintaining the source code of computer programs. This source code is written in one or more programming languages. The purpose of programming is to create a program that performs specific operations or exhibits a...
, eager evaluation or greedy evaluation is the evaluation strategy
Evaluation strategy
In computer science, an evaluation strategy is a set of rules for evaluating expressions in a programming language. Emphasis is typically placed on functions or operators: an evaluation strategy defines when and in what order the arguments to a function are evaluated, when they are substituted...
in most traditional programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....
s. In eager evaluation an expression
Expression (mathematics)
In mathematics, an expression is a finite combination of symbols that is well-formed according to rules that depend on the context. Symbols can designate numbers , variables, operations, functions, and other mathematical symbols, as well as punctuation, symbols of grouping, and other syntactic...
is evaluated as soon as it gets bound to a variable
Variable (programming)
In computer programming, a variable is a symbolic name given to some known or unknown quantity or information, for the purpose of allowing the name to be used independently of the information it represents...
. The term is typically used to contrast lazy evaluation
Lazy evaluation
In programming language theory, lazy evaluation or call-by-need is an evaluation strategy which delays the evaluation of an expression until the value of this is actually required and which also avoids repeated evaluations...
, where expressions are only evaluated when evaluating a dependent expression. Eager evaluation is almost exclusively used in imperative
Imperative programming
In computer science, imperative programming is a programming paradigm that describes computation in terms of statements that change a program state...
programming languages where the order of execution is implicitly defined by the source code organization.
One advantage of eager evaluation is that it eliminates the need to track and schedule the evaluation of expressions. It also allows the programmer to dictate the order of execution, making it easier to determine when sub-expressions (including functions) within the expression will be evaluated, as these sub-expressions may have side-effects that will affect the evaluation of other expressions.
A disadvantage of eager evaluation is that it forces the evaluation of expressions that may not be necessary at run time, or it may delay the evaluation of expressions that have a more immediate need. It also forces the programmer to organize the source code for optimal order of execution.
Note that many modern compilers are capable of scheduling execution to better optimize processor resources and can often eliminate unnecessary expressions from being executed entirely. Therefore, the notions of purely eager or purely lazy evaluation may not be applicable in practice.
See also
- Controlling eagerness in lazy languages
- Evaluation strategyEvaluation strategyIn computer science, an evaluation strategy is a set of rules for evaluating expressions in a programming language. Emphasis is typically placed on functions or operators: an evaluation strategy defines when and in what order the arguments to a function are evaluated, when they are substituted...
- Graph reductionGraph reductionIn computer science, graph reduction implements an efficient version of non-strict evaluation, an evaluation strategy where the arguments to a function are not immediately evaluated. This form of non-strict evaluation is also known as lazy evaluation and used in functional programming languages...
- Lazy evaluationLazy evaluationIn programming language theory, lazy evaluation or call-by-need is an evaluation strategy which delays the evaluation of an expression until the value of this is actually required and which also avoids repeated evaluations...