[Haskell-cafe] Re: Proper way to write this

Jacques Carette carette at mcmaster.ca
Tue Dec 27 15:02:44 EST 2005



Max Vasin wrote:

> Pupeno wrote:
>
>> What about this
>>
>> runDaytimeServer :: DaytimeServer -> IO DaytimeServer
>> runDaytimeServer dts = do
>>   dts' <- runStreamDaytimeServer dts
>>   dts' <- runDgramDaytimeServer dts'
>>   return dts'
>
>
> runDaytimeServer dts
>   = runStreamDaytimeServer dts >>= runDgramDaytimeServer

I have seen this pattern many times, and I am surprised it is not 
present in Control.Monad.

(>>-) :: (a -> m b) -> (b -> m c) -> a -> m c
(>>-) f g a = f a >>= g

Your code becomes
runDaytimeServer = runStreamDaytimeServer >>- runDgramDaytimeServer

This >>- operator is a very natural "composition" operator in a Monad, 
at least to me.  It seems to be the most obvious counter-part to '.'.  
But I could not find anything with that signature in the libraries -- or 
did I miss it?

[And >>- is typographically sub-optimal, but I can't think of something 
better].

Jacques


More information about the Haskell-Cafe mailing list