[Haskell-cafe] Practical Haskell question.
wss at cs.nott.ac.uk
Mon Jun 25 05:13:28 EDT 2007
On 25 Jun 2007, at 06:39, Michael T. Richter wrote:
> 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 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,
More information about the Haskell-Cafe