[Haskell-cafe] Tying a simple circularly STM linked list

John Ky newhoggy at gmail.com
Tue Jan 6 18:05:56 EST 2009


Hi,

I've written a circularly linked list, but there is some code in it I feel
is redundant, but don't know how to get rid of:

-- Transactional loop.  A loop is a circular link list.
data Loop a
   = ItemLink
      { item :: a
      , prev :: TVar (Loop a)
      , next :: TVar (Loop a)
      }
   | InitLink

-- Create a new empty transactional loop.
newLoop :: a -> STM (TVar (Loop a))
newLoop item = do
   tLoop <- newTVar InitLink
   writeTVar tLoop (ItemLink item tLoop tLoop)
   return tLoop

In the above, the InitLink value is only ever used in the newLoop function
to create a single one element circular linked list.  Is there a way to
write newLoop to avoid using this value?

Thanks

-John
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090107/53ec2cae/attachment.htm


More information about the Haskell-Cafe mailing list