[Haskell-cafe] Re: monad subexpressions

Claus Reinke claus.reinke at talk21.com
Fri Aug 3 16:30:14 EDT 2007


> I'll dig for it later if you like.  The essence of the matter was a 
> bunch of functions that looked something like this:
> 
> foo = do b' <- readTVar b
>         c' <- readTVar c
>         d' <- readTvar d
>         return (b' + c' / d')
> 
> In other words, a string of readTVar statements, followed by one 
> computation on the results.  Each variable name has to be duplicated 
> before it can be used, and the function is four lines long instead of 
> one.

if that happens frequently, an instance of the numeric classes 
seems called for, automating both the lifting and the readTVar,
but if there are only a couple of cases, you could lift the operations 
for the module, or even per definition:

    foo1 b c d = readTVar b + readTVar c / readTVar d
      where (+) = liftM2 (Prelude.+)
            (/) = liftM2 (Prelude./)

claus


More information about the Haskell-Cafe mailing list