为什么懒惰评估有用? [英] Why is lazy evaluation useful?
问题描述
注意:我不是指记忆。
主要是因为它可以更高效 - 如果它们不会被计算,则不需要计算它们的值用过的。例如,我可以将三个值传递给函数,但根据条件表达式的顺序,实际上只能使用一个子集。在像C这样的语言中,无论如何都要计算所有三个值。但在Haskell中,只计算了必要的值。
它也允许像无限列表这样的酷东西。我不能在像C这样的语言中拥有无限的列表,但在Haskell中,这没有问题。无限列表经常在某些数学领域中使用,所以有能力操作它们是有用的。
I have long been wondering why lazy evaluation is useful. I have yet to have anyone explain to me in a way that makes sense; mostly it ends up boiling down to "trust me".
Note: I do not mean memoization.
Mostly because it can be more efficient -- values don't need to be computed if they're not going to be used. For example, I may pass three values into a function, but depending on the sequence of conditional expressions, only a subset may actually be used. In a language like C, all three values would be computed anyway; but in Haskell, only the necessary values are computed.
It also allows for cool stuff like infinite lists. I can't have an infinite list in a language like C, but in Haskell, that's no problem. Infinite lists are used fairly often in certain areas of mathematics, so it can be useful to have the ability to manipulate them.
这篇关于为什么懒惰评估有用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!