[Haskell-cafe] Re: Verbosity of imperative code
Scherrer, Chad
Chad.Scherrer at pnl.gov
Wed Dec 7 16:26:33 EST 2005
> On Tue, Dec 06, 2005 at 10:58:45PM +0300, Bulat Ziganshin wrote:
> > the third-priority problem is language itself. in particular, i hate
> > Haskell school of imperative manipulations:
> >
> > x' <- readIORef x
> > y' <- readIORef y
> > writeIORef z (x'*y')
Here's a way to make some of this less messy:
class PlusEq a b m | a -> m where
(+=) :: a -> b -> m ()
instance (Num a) => PlusEq (IORef a) a IO where
xRef += y = do x <- readIORef xRef
writeIORef xRef (x + y)
instance (Num a) => PlusEq (IORef a) (IORef a) IO where
xRef += yRef = do y <- readIORef yRef
xRef += y
Then instead of
do x <- xRef
y <- yRef
writeIORef yRef (x + y)
you can just say y += x. I've started on an InPlace module to do things
like this in general.
-Chad Scherrer
More information about the Haskell-Cafe
mailing list