This is a good post in that it illustrates how it's possible to write useful Haskell that interacts with the real world without weaving IO through the entire codebase (as many beginners end up doing).<p>I still find myself preferring to use a dependency injection style when writing Haskell. The code ends up looking similar, and more flexible, in my opinion (you can inject different dependencies based on runtime values).<p>Here is an example using the method described in the article vs. dependency injection to print out the current time.<p>Article:<p><pre><code> class MonadTime m where
getTime :: m Integer
class MonadPrint m a where
doPrint :: a -> m ()
instance MonadTime IO where
getTime = do
TOD s _ <- getClockTime
return s
instance Show a => MonadPrint IO a where
doPrint = print
printTime :: (Monad m, MonadPrint m Integer, MonadTime m) => m ()
printTime = getTime >>= doPrint
main :: IO ()
main = printTime
</code></pre>
Dependency injected:<p><pre><code> data MonadPrint m a = MonadPrint { doPrint :: a -> m () }
data MonadTime m = MonadTime { getTime :: m Integer }
ioMonadTime :: IO Integer
ioMonadTime = do
TOD s _ <- getClockTime
return s
printTime :: Monad m => MonadPrint m Integer -> MonadTime m -> m ()
printTime MonadPrint{..} MonadTime{..} = getTime >>= doPrint
main :: IO ()
main = printTime (MonadPrint print) (MonadTime ioMonadTime)
</code></pre>
Good read on the subject of being careful with overusing typeclasses:<p><a href="http://www.haskellforall.com/2012/05/scrap-your-type-classes.html" rel="nofollow">http://www.haskellforall.com/2012/05/scrap-your-type-classes...</a>