Haskell
中对表达式的计算使用的是Lazy evaluation
(也称之为nonstrict evaluation
):即只有表达式的值真正需要时,才会被计算。Haskell
中把用来跟踪还没有计算的表达式的记录称之为trunk
。
参考自stackoverflow:
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.
Lazy evaluation
的优点:高效(延迟执行代码),支持像infinite lists
这样的cool stuff
。