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.