replicate returns a list of IO String actions. To complete these steps, they must be run in the IO monad. Therefore, you do not want to join the array of I / O operations, but simply run them sequentially and return the result.
Here is what i will do
readNLines :: Int -> IO String readNLines n = do lines <- replicateM n getLine return $ concat lines
Or in the applicative style:
import Control.Applicative readNLines :: Int -> IO String readNLines n = concat <$> replicateM n getLine
Both of them use monadic replication (replicateM), which evaluates a list of monadic values ββin a sequence, and not just returns a list of actions
source share