chains of IORefs

Simon Marlow
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.
> eg, the following toy code creates a chain of 1000 linked records.
> import IOExts
> data Record=3D RecordNil
>            | Record String Int Chain
> type Chain =3D IORef Record
> main=3DmakeChain [1..1000]
> makeChain::[Int]->IO Chain
> makeChain     []=3DnewIORef RecordNil
> makeChain (a:as)=3DmakeChain as>>=3D \chain->
>                      newIORef (Record "data" a chain)
> such structures will be garbage-collected "automatically", right?


> 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
> type Chain=3DIO (IORef Record)
> 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