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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access