In Haskell, lazy-IO actions return their result before the IO is completely performed. For instance, getContents returns a lazy string which will cause the file to be actually read only when characters are accessed from the string. Since it is hard to predict when the IO will actually happen, lazy-IO is fragile and must be used with some care. Popular deterministic alternatives are conduit and pipes.
Questions tagged [lazy-io]
18 questions
8
votes
1 answer
What caused this "delayed read on closed handle" error?
I just installed GHC from the latest sources, and now my program gives me an error message about a "delayed read on closed handle". What does this mean?

dfeuer
- 48,079
- 5
- 63
- 167
6
votes
3 answers
Reimplementing getContents using getChar
On my journing towards grasping lazy IO in Haskell I tried the following:
main = do
chars <- getContents
consume chars
consume :: [Char] -> IO ()
consume [] = return ()
consume ('x':_) = consume []
consume (c : rest) = do
putChar c
consume…

johanneslink
- 4,877
- 1
- 20
- 37
6
votes
4 answers
Haskell: Hiding failures in lazy IO
This is a noob question.
I'd like to write a function which provides a lazy stream of images, presumably something like:
imageStream :: [IO Image]
Unfortunately, the function which reads images can fail, so it looks like:
readImage :: IO (Maybe…

emchristiansen
- 3,550
- 3
- 26
- 40
5
votes
1 answer
Error reading and writing same file simultaneously in Haskell
I need to modify a file in-place. So I planned to read file contents, process them, then write the output to the same file:
main = do
input <- readFile "file.txt"
let output = (map toUpper input)
-- putStrLn $ show $ length output
writeFile…

Rogach
- 26,050
- 21
- 93
- 172
5
votes
2 answers
Lazy output from monadic action
I have the next monad transformer:
newtype Pdf' m a = Pdf' {
unPdf' :: StateT St (Iteratee ByteString m) a
}
type Pdf m = ErrorT String (Pdf' m)
Basically, it uses underlying Iteratee that reads and processes pdf document (requires…

Yuras
- 13,856
- 1
- 45
- 58
3
votes
1 answer
Why is gnuplot plot not receiving the entirety of stdin from getContents?
I'm having an issue with lazy IO, but I don't know how to fix it.
I've got three small test programs here, but with V2 being the thing I actually want.
Somewhere, it seems that either getContents is being halted early, or gnuplot is finishing…

Squidly
- 2,707
- 19
- 43
3
votes
2 answers
Preventing "getCurrentDirectory: resource exhausted (Too many open files)" error
I am trying to run a Parsec parser over a whole bunch of small files, and getting an error saying I have too many open files. I understand that I need to use strict IO, but I'm not sure how to do that. This is the problematic code:
files =…

Drew
- 12,578
- 11
- 58
- 98
3
votes
1 answer
How does hGetContents achieve memory efficiency?
I want to add Haskell to my toolbox so I'm working my way through Real World Haskell.
In the chapter in Input and Output, in the section on hGetContents, I came across this example:
import System.IO
import Data.Char(toUpper)
main :: IO ()
main = do…

Marcel
- 367
- 1
- 12
3
votes
2 answers
Haskell lazy Bytestring words not lazy?
I have the following Haskell program for computing a maximum sum substring of a string of integers:
{-# LANGUAGE BangPatterns #-} {-# OPTIONS_GHC -O2 #-}
import Data.Functor
import Data.Maybe
import Data.ByteString.Lazy.Char8…

user1531083
- 750
- 6
- 15
2
votes
1 answer
Why doesn't print force entire lazy IO value?
I'm using http-client tutorial to get response body using TLS connection. Since I can observe that print is called by withResponse, why doesn't print force entire response to the output in the following fragment?
withResponse request manager $…

sevo
- 4,559
- 1
- 15
- 31
2
votes
1 answer
http-conduit, snap and lazy IO
I have two http-servers working with a json api using the snap framework
my first prototype contains a handler similar to this example handler
import Data.ByteString (ByteString)
import Data.ByteString.Char8 as B (unwords,…

epsilonhalbe
- 15,637
- 5
- 46
- 74
1
vote
2 answers
Forcing evaluation on lazy IO
My program reads a line from a network socket and writes it to disc. Since lines can be really long and strings had terrible performance I started using lazy byte strings. Now it seems that Haskell will go past hClose on disc file handle without…

Marek Sapota
- 20,103
- 3
- 34
- 47
1
vote
0 answers
write JSON to file as lazy ByteString directly from Aeson encode output without converting to String
Currently, I'm using something like this to write JSON content (my_json) to a file (my_output_filepath):
import Data.Aeson.Encode.Pretty
import qualified Data.ByteString.Lazy.UTF8 as U
writeFile my_output_filepath $ U.toString $…

kostmo
- 6,222
- 4
- 40
- 51
1
vote
1 answer
`interact` using Text instead of String
I'd like to rewrite the interact function, but using Text instead of String. Is it possible to use Data.Text and/or Data.Text.Lazy to accomplish the same behavior as interact?
For example, when I run this program using String:
main = interact…

Dan Burton
- 53,238
- 27
- 117
- 198
0
votes
1 answer
philosophy behind http-simple setRequestBodyLBS
I am trying to develop an http client by using http-simple library. Some implementation of the library seems confusing to me.
This library makes heavy use of Conduit; however there is also this 'setRequestBodyLBS' function and interestingly, the…

user2812201
- 437
- 3
- 7