Input Processing Loops in Haskell
Simon Marlow
simonmar@microsoft.com
Tue, 13 Nov 2001 17:02:54 -0000
> I apologize if this is a bit off-topic -- I'm asking it here because
> if what I want to do is possible, it may only be possible with ghc
> extensions, as opposed to vanilla Haskell......
>=20
> In many types of applications, it's common to have a loop which=20
> loops infinitely, of at least until some condition is met. For
> example, in C-like syntax:
>=20
> for (;;;) {
> a =3D get_input();
> if (a =3D=3D GET_OUT) break;
> do_comthing();
> }
You can paraphrase this in Haskell, in the IO monad, like this:
loop =3D do {
a <- get_input;
if (a =3D=3D gET_OUT) then return () else loop;
}
In GHC this won't grow any stack each time around the loop, even with
optimisation off. It depends on the actual implementation of the IO
monad, but I imagine the other Haskell implementations will also have
this property.
Cheers,
Simon