by wvs · 13y ago · 0 comments
(.) composes two functions[1] but (>>=) composes a monad value with a monad function[2]. It'd be nice if we had an operator to compose two monad functions instead.
We can define that operator in terms of (>>=): "g <=< f = \x -> f x >>= g". You can check that the types work out[3]. Notice the great similarity to (.). (It's because they're both categorical composition operators of functions.)
Now we can write "half <=< half $ 8" and everything's dandy.
[1]: (.) :: (b -> c) -> (a -> b) -> (a -> c)
[2]: (>>=) :: m a -> (a -> m b) -> m b
[3]: (<=<) :: (b -> m c) -> (a -> m b) -> (a -> m c)I think one reason for "monad hell" is unawareness about this kind of composable style.