chains of IORefs

Simon Marlow simonmar@microsoft.com
Thu, 6 Feb 2003 14:09:19 -0000


> using IORefs, one can construct linked lists in Haskell
> that resemble one's used in imperative C programming.
>=20
> eg, the following toy code creates a chain of 1000 linked records.
>=20
> import IOExts
>=20
> data Record=3D RecordNil
>            | Record String Int Chain
> type Chain =3D IORef Record
>=20
> main=3DmakeChain [1..1000]
>=20
> makeChain::[Int]->IO Chain
> makeChain     []=3DnewIORef RecordNil
> makeChain (a:as)=3DmakeChain as>>=3D \chain->
>                      newIORef (Record "data" a chain)
>=20
> such structures will be garbage-collected "automatically", right?

Yes.

> in general terms, please comment on how well GHC supports code
> that uses such structures.

It should work fine.

> now, to turn makeChain into a "lazy producer" requires adding an IO
> to the type of Chain, yielding=20
>=20
> type Chain=3DIO (IORef Record)
>=20
> please comment on how well GHC would tend to support such a type.

I'm not sure exactly what you mean by "support" in this context, but I'm
not aware of any known bugs that would impact your use of the above
type.

Cheers,
	Simon