IO and fold (was Re: fold on Monad? )

Jon Fairbairn Jon.Fairbairn@cl.cam.ac.uk
Thu, 30 May 2002 16:11:04 +0100


> >Yes you can define it, =

> =

> And you can, as well.

Man sollte sich nicht darauf verlassen, da=DF ein Englander
"man" verwendet, wenn es angebraucht w=E4re=B9.

> That's how common idioms come into being;
> there's no special magic about the folds already in existence.

Well, my point is that there is -- indeed some ways of
defining the type for lists use a fold as the starting
point. I mentioned the lack here because I think there is
some deeper structure that someone cleverer than me may be
able to see.

> =

> >It seems to me that there's something odd about the way the
> >IO monad interacts with bulk operations on files.  =

> =

> That may be more related to IO than to the monadic approach
> to it =


Yes. Hence the change of subject line. =


> >In particular, it seems odd that getContents should ever be the
> >easiest way of tackling something, =

> =

> Who says that?

I did! I think getContents is a klugey hangover from stream
based I/O, and the existence of "semi-closed handles"
supports that, so I'm hoping that people might become
interested in looking at the question.

> >One of the great things about fold is that you don't have to
> >code the test for the end: it's encapsulated in the
> >higher-order function. Shouldn't there be the same for IO?
> =

> Isn't it?
> =

>     getContents =3D foldX (++) "" -- we drop the newlines here..
>     getLines =3D foldX (:) []
> =

> No eof-testing in sight.

Sure, once we've defined foldX, but then we need to know
whether it really is a fold &c.  I should remember to make
my messages to the list less indirect.

> So we've mostly got a fold lifted into the io-monad, with getLine
> delivering the "head" (and implicitly truncating further input to
> the "tail"). What more do you want?-)

Theory! That's what I want!

Cheers,

   J=F3n

1. I had to get help with this; perhaps it would have been
better to leave my numerous mistakes in it!

-- =

J=F3n Fairbairn                                 Jon.Fairbairn@cl.cam.ac.u=
k
31 Chalmers Road                                         jf@cl.cam.ac.uk
Cambridge CB1 3SZ            +44 1223 570179 (after 14:00 only, please!)