[Haskell-beginners] Question about IO, particularly hGetContents

Isaac Dupree ml at isaac.cedarswampstudios.org
Fri Apr 2 14:13:39 EDT 2010

On 04/02/10 10:28, Kyle Murphy wrote:
> I'm a bit of a beginner so I might be wrong, but I think do only forces
> evaluation at the level of the do block, not recursively. Think of it this
> way, you've got a series of function calls, which are represented as thunks,
> and which in turn return more thunks. If you use do notation to execute that
> series of functions they all get evaluated in order, but you're still left
> with more un-evaluated thunks because that's what the functions returned.

The only reason hGetContents was able to return thunks with 
not-yet-completed IO is because it cheated and used unsafeInterleaveIO. 
  "hGetContents" is one of the worst functions you could possibly pick 
to learn the basic semantics of Haskell IO.

Normally, an IO-action being executed in sequence will force all the 
input and output it contains to complete, though some pure computation 
might remain not-yet-evaluated (say, sorting or summing a list that 
you'd retrieved from IO...)


More information about the Beginners mailing list