开发者

Monads: What's the difference between seq and >>=?

开发者 https://www.devze.com 2023-03-23 16:24 出处:网络
What\'s the differ开发者_开发百科ence? Does seq guarantee more flow conditions?They aren\'t related at all.

What's the differ开发者_开发百科ence? Does seq guarantee more flow conditions?


They aren't related at all.

seq has the type:

seq :: a -> b -> b

It is used (as seq a b, or a `seq` b) to evaluate a to head normal form, which is a fancy way of saying that it forces the lazy value a to be evaluated a little bit. It has nothing to do with monads.

>>= is for sequencing monads. It has the type:

(>>=) :: Monad m => m a -> (a -> m b) -> m b

It is used to get the value from a monadic value and pass it to a function that returns another monadic value. Basically something like:

getLine >>= putStrLn

which would get a string of input from the command-line and then print it out.

So, basically, no relation at all.


seq is not specific to monads. seq is used to force evaluation of its first argument before its second is returned.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号