Time cost of Haskell `seq` operator

前端 未结 5 1276
逝去的感伤
逝去的感伤 2021-02-13 22:32

This FAQ says that

The seq operator is

seq :: a -> b -> b

x seq y will evaluate x, enough to chec

5条回答
  •  不要未来只要你来
    2021-02-13 23:29

    Of course seq by itself does not "evaluate" anything. It just records the forcing order dependency. The forcing itself is triggered by pattern-matching. When seq x (f x) is forced, x will be forced first (memoizing the resulting value), and then f x will be forced. Haskell's lazy evaluation means it memoizes the results of forcing of expressions, so no repeat "evaluation" (scary quotes here) will be performed.

    I put "evaluation" into scary quotes because it implies full evaluation. In the words of Haskell wikibook,

    "Haskell values are highly layered; 'evaluating' a Haskell value could mean evaluating down to any one of these layers."

    Let me reiterate: seq by itself does not evaluate anything. seq x x does not evaluate x under any circumstance. seq x (f x) does not evaluate anything when f = id, contrary to what the report seems to have been saying.

提交回复
热议问题