[Haskell-cafe] Practical Haskell question.

Wouter Swierstra wss at cs.nott.ac.uk
Mon Jun 25 05:13:28 EDT 2007


Hi Michael,

On 25 Jun 2007, at 06:39, Michael T. Richter wrote:
>
> do
>   x <- performActionA
>   y <- performActionB
>   z <- performActionC
>   return $ calculateStuff x y z

I don't know about you're exact example, but here's what I'd do.

Control.Monad has functions like when, unless, and guard that you can  
use to check whether the "precondition" holds. I find an "ifM"  
combinator quite useful sometimes:

ifM :: Monad m => m Bool -> m a -> m a -> ma
ifM cond thenBranch elseBranch = do
   b <- cond
   if cond
     then thenBranch
     else elseBranch

If everything checks out, you can then execute your A, B, and C actions.

I don't think you really want arrows here. The right idiom is  
applicative functors (see Control.Applicative). You could then write  
the above as:

calculateStuff <$> x <*> y <*> z

Hope this helps,

   Wouter



More information about the Haskell-Cafe mailing list