Eager evaluation/applicative order and lazy evaluation/normal order

后端 未结 3 1419
[愿得一人]
[愿得一人] 2021-02-05 11:05

As far as I know, eager evaluation/applicative order evaluates all arguments to a function before applying it, on the other hand, lazy evaluation/normal order evaluates the argu

3条回答
  •  无人及你
    2021-02-05 11:39

    From Kevin Sookocheff's Normal, Applicative and Lazy Evaluation post (emphases, stylistic changes mine):

    Lazy Evaluation

    While normal-order evaluation may result in doing extra work by requiring function arguments to be evaluated more than once, applicative-order evaluation may result in programs that do not terminate where their normal-order equivalents do. In practise, most functional programming languages solve this problem using lazy evaluation.

    With lazy evalution, we delay function evaluation in a way that avoids multiple evaluations of the same function — thus combining the benefits of normal-order and applicative-order evaluation.

    With lazy evaluation, we evaluate a value when it is needed, and after evaluation all copies of that expression are updated with the new value. In effect, a parameter passed into a function is stored in a single location in memory so that the parameter need only be evaluated once. That is, we remember all of the locations where we a certain argument will be used, and when we evaluate a function, we replace the argument with the value.

    As a result, with lazy evaluation, every parameter is evaluated at most once.

    This was too long to post as a comment beneath the question, and updating existing answers with it seemed inappropriate, hence this answer.

提交回复
热议问题