A better way to map a function that requires IO over a list

前端 未结 4 1506
感动是毒
感动是毒 2021-01-20 03:40

So lately I have a list of strings, and need to independently go over each one and perform some IO function.

So basically what I have is this:

4条回答
  •  Happy的楠姐
    2021-01-20 04:21

    Your goOverList function is almost equivalent to mapM_ putStrLn. (Just almost because mapM_ also works with the empty list while your function does not).

    mapM is a function that applies a function of type a -> IO b¹ to each item in a list of as and gives you back an IO² with a list of bs. mapM_ is the same as mapM except that it doesn't store the results in a list (which doesn't make sense for actions that return () like putStrLn does).

    ¹ Actually it's more general than that: The function has type a -> m b where Monad m, but in this case m is IO.

    ² Again it's actually m.

提交回复
热议问题