Is it possible to have a lazy version of `sequence` without using` unsafeIO `?

If I have a possibly endless list of IO-monads, and I'm sure that their subsequent execution will not be affected by other IOs, can I somehow make it lazily ordered (evaluated)?

To clarify my point, here is some pseudo-Haskell code demonstrating what I had in mind:

main = do
    inputs <- sequence . repeat $ getLine -- we are forever stuck here
    mapM_ putStrLn inputs -- not going to run

Now I know that in the above example we can just use getContentsto get the effect I want

main = do
    inputs <- return . lines =<< getContents
    mapM_ putStrLn inputs

but in my application monads are IOnot getLine, but an external function get1 :: IO (Maybe Record). However, this actually brings my point of view, because, apparently, it getContentsuses unsafeIOto achieve this lazy effect. My question is what is needed? (If you are interested in what exactly I want to do, refer to this question .)

+4
source share
2 answers

Perhaps you are looking for this?

main = do
  let inputs = repeat getLine
  mapM_ (>>=putStrLn) inputs
0
source

Just to call the cat: No.

- IO a, - a IO. , act :: IO a , , .

unsafeInterleaveIO , , .

0

Source: https://habr.com/ru/post/1612090/


All Articles