Are handles closed automatically when they fall out of scope?

David Brown haskell at
Mon Oct 25 18:11:49 EDT 2004

On Mon, Oct 25, 2004 at 09:42:13PM +0000, John Goerzen wrote:

> > Maybe I misunderstood something ... but why do you need to
> > read from the stream multiple times after calling
> > hGetContents? The function returns the _entire_ (lazily
> > evaluated) input stream, there is no need to read again. You
> > already _have_ everything.
> Because I'd have to use a state monad or something to chop off the top
> of the list each time I read from it.  This is a FTP client module, so,
> for instance, you might see this session:
> cwd h "/pub/linuc"
> nlst h Nothing
> etc.
> Each command sends something and reads a reply.  "h" holds an instance
> of that data structure I posted.
> If I just try to read from the list during each command, I would just
> read the same (first) response over and over again.  Or, I'd have to
> store a position (which also requires a state monad), which would lead
> to wasting memory keeping all that stuff around.

Sounds to me like you really want to be putting the code doing this inside
of the IO monad.  It is interacting with the outside world, and maintaining
state at that...


More information about the Glasgow-haskell-users mailing list