Are handles closed automatically when they fall out of scope?

John Goerzen jgoerzen at
Mon Oct 25 17:42:13 EDT 2004

On 2004-10-25, Peter Simons <simons at> wrote:
> John Goerzen writes:
> > Now, if [I read with hGetContents h], then the first read
> > I try to make using it works, but subsequent ones don't,
> > since the first one made it half-closed already.
> 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


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.

More information about the Glasgow-haskell-users mailing list