为什么懒惰评估有用? [英] Why is lazy evaluation useful?

查看:91
本文介绍了为什么懒惰评估有用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在想,为什么懒惰的评估是有用的。我还没有任何人以一种合理的方式向我解释;大多数情况下它最终会让人相信我。



注意:我不是指记忆。

解决方案

主要是因为它可以更高效 - 如果它们不会被计算,则不需要计算它们的值用过的。例如,我可以将三个值传递给函数,但根据条件表达式的顺序,实际上只能使用一个子集。在像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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆