且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

惰性评估的优点是什么?

更新时间:2023-11-20 20:23:52

这是对语法树的评估.如果您懒惰地评估语法树(即当需要它表示的值时),则必须将其完整地进行整个计算的前面步骤.这是惰性评估的开销.但是,有两个优点. 1)如果从不使用结果,则不会不必要地撤消该树,并且2)您可以以某种递归形式表达和使用无限语法树,因为您只会将其评估到所需的深度,而不是评估(急切地)将其全部删除,这是不可能的.

This refers to the evaluation of a syntax tree. If you evaluate a syntax tree lazily (i.e. when the value it represents is needed), you must carry it through the preceeding steps of your computation in its entirety. This is the overhead of lazy evaluation. However, there are two advantages. 1) you will not evaulate the tree unnecessarily if the result is never used, and 2) you can express and use an infinite syntax tree in some recursive form, because you will only ever evaluate it to the depth you need, as opposed to evaluating (eagerly) in its entirety, which would be impossible.

我不了解haskel,但据我所知,像python或ML这样的编程语言都非常重视评估.例如,要在ML中模拟延迟评估,必须创建一个不带参数但返回结果的伪函数.然后,该函数就是您的语法树,您可以随时通过使用空的参数列表对其进行懒惰地求值.

I don't know haskel, but as far as I know programming languages like python or ML evaluate eagerly. For example to simulate lazy evaluation in ML, you must create a dummy function that takes no parameters but returns the result. This function is then your syntax tree that you can lazily evaluate at any time by invoking it with an empty argument list.