Mapping IO Values with fmap

So far we’ve talked a lot about combining functions that both do IO, but we haven’t talked about a common problem: what do you do when you have a function that doesn’t know (or care) about IO, and you want to use it on an IO value?

Let’s think about command line parsing, which can be one of the most common cases where we might run into this situation. Say we wanted to write an application that would add up a list of integers the user provides as command line arguments. We can use the getArgs function from the System.Environment module in base to get a list of all of the command line arguments as a list of strings, but we’ll still need to convert those strings to integers and add them up. Let’s say that we have a function ...

Get Effective Haskell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.