turn off lazy evaluation in haskell

前端 未结 7 2027
长发绾君心
长发绾君心 2020-12-14 19:03

Is it possible to turn off lazy evaluation in Haskell?

Is there a specific compiler flag of library to facilitate this?

I wanted to try something new with

7条回答
  •  独厮守ぢ
    2020-12-14 19:29

    The strict-identity package has a strict version of the Identity monad.

    You can find it here: https://hackage.haskell.org/package/strict-identity

    The usage would look something like this:

    foo = runStrictIdentity $! do
        x <- f a b
        y <- g x y
        return $! x + y
    

    Each time return or bind >>= is used, the two parts are evaluated using seq, giving a reasonable guarantee of strictness provided your data structure isn't too deep. This works, for i.e. numbers and basic structures.

提交回复
热议问题