RE: [Haskell] ANNOUNCING: The Haskell Bookstore
"Simon Marlow" <[email protected]> Fri, 25 Jun 2004 12:14:26 +0100
| Newsgroups | gmane.comp.lang.haskell.ffi |
|---|---|
| Message-ID | <3429668D0E777A499EE74A7952C382D1020BA3E2@EUR-MSG-01.europe.corp.microsoft.com> |
On 25 June 2004 11:30, Daan Leijen wrote: > Is there anyone who knows why "atomicModifyIORef" has type: > > IORef a -> (a -> (a, b)) -> IO b (1) > > Instead of: > > IORef a -> (a -> a) -> IO a (2) > > > It seems to me that returning the old value is always good > enough right? Here is an implementation of "atomicModifyIORef" > with the current type in terms of a function "proposedModifyIORef" > with type (2). > > atomicModifyIORef :: IORef a -> (a -> (a,b)) -> IO b > atomicModifyIORef ref f > = do old <- proposedModifyIORef ref (fst . f) > return (snd (f old)) Yes, but in your version you apply f to the old value twice, potentially duplicating an expensive computation. atomicModifyIORef isn't part of the FFI spec, BTW. Cheers, Simon