Posts

Showing posts from December, 2014

LTMT Part 3: The Monad Cookbook

IntroductionThe previous twoposts in my Less Traveled Monad Tutorial series have not had much in the way of directly practical content. In other words, if you only read those posts and nothing else about monads, you probably wouldn't be able to use monads in real code. This was intentional because I felt that the practical stuff (like do notation) had adequate treatment in other resources. In this post I'm still not going to talk about the details of do notation--you should definitely read about that elsewhere--but I am going to talk about some of the most common things I have seen beginners struggle with and give you cookbook-style patterns that you can use to solve these issues.Problem: Getting at the pure value inside the monadThis is perhaps the most common problem for Haskell newcomers. It usually manifests itself as something like this: main = do lineList <- lines $ readFile "myfile.txt" -- ... do something with lineList here That code generates …