Haskell`seq`运营商的时间成本 [英] Time cost of Haskell `seq` operator
问题描述
此常见问题解答表示
seq运算符是
seq :: a - > b - > b
x
seq
足以检查它是不是底部,然后
丢弃结果并评估y。这看起来似乎不太有用,但它
意味着x在保证y被考虑之前保证被评估。
这太糟糕了Haskell不错,但这是否意味着在
x`seq` fx
评估 x
的费用将支付两次(放弃结果)? seq
函数将放弃<$ c $>的值。 c> x ,但是由于已经评估过该值,因此所有对 x
的引用都将更新为不再指向未评估版本的 x
,而是指向评估版本。因此,尽管 seq
评估并放弃了 x
,但其值为 x
,因此不会重复评估。
This FAQ says that
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.
That's awfully nice of Haskell, but does it mean that in
x `seq` f x
the cost of evaluating x
will be paid twice ("discard the result")?
The seq
function will discard the value of x
, but since the value has been evaluated, all references to x
are "updated" to no longer point to the unevaluated version of x
, but to instead point to the evaluated version. So, even though seq
evaluates and discards x
, the value has been evaluated for other users of x
as well, leading to no repeated evaluations.
这篇关于Haskell`seq`运营商的时间成本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!