How to flatten IO [[String]]?

前端 未结 2 735
失恋的感觉
失恋的感觉 2020-12-18 06:14

I am new to Haskell and have some difficulties wrapping my head around some of it\'s concepts.

While playing around with IO I wanted to flatten an IO [[String]].

相关标签:
2条回答
  • 2020-12-18 06:28

    You can't get anything "out" of IO, so what you need to do is lift flatten to work inside it. The simplest way to do this is fmap--just like map applies a function over a list, fmap applies a function over any Functor instance, such as IO.

    flattenIO xs = fmap flatten xs
    

    In more general cases, you can use do notation to get at things in IO computations. For example:

    flattenIO xs = do ys <- xs
                      return (flatten ys)
    

    ...which is just a roundabout way of writing fmap in this case.

    0 讨论(0)
  • 2020-12-18 06:41

    You have to realize what IO something means. It's not a something, it's an action that will return a something (In this case, something is [[String]]). So, you cannot do anything with the thing that the action returns, until you perform the action, which returns that thing.

    You have two options to solve your problem.

    1. Perform the action, and use the result. This is done like this:

      do
        ds <- domains       -- Perform action, and save result in ds
        return $ flatten ds -- Flatten the result ds
      
    2. Create a new action that takes the result of some action, and applies a function to it. The new action then returns the transformed value. This is done with the liftM function in the Control.Monad module.

      import Control.Monad
      -- ...
      
      do
        -- Creates a new action that flattens the result of domains
        let getFlattenedDomains = liftM flatten domains
      
        -- Perform the new action to get ds, which contains the flattened result
        ds <- getFlattenedDomains
      
        return ds
      

    PS. You might want to rename your domains variable to getDomains to clarify what it does. It's not a pure value; it's a monadic action that returns a pure value.

    0 讨论(0)
提交回复
热议问题