The seq operator is
seq :: a -> b -> b
x
seq
y will evaluate x, enough to check that it is not bottom, then discard the result and evaluate y. This might not seem useful, but it means that x is guaranteed to be evaluated before y is considered.
这对 Haskell 来说非常好,但这是否意味着在
x `seq` f x
评估成本
x
将支付两次(“丢弃结果”)?
最佳答案
seq
函数将丢弃 x
的值,但由于该值已被评估,所有对 x
的引用被“更新”为不再指向 x
的未评估版本,而是指向评估的版本。所以,即使 seq
评估和丢弃 x
, 该值已为 x
的其他用户评估过同样,导致没有重复评估。
关于haskell - Haskell `seq` 算子的时间成本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9707190/