[Haskell-cafe] Shortening if-then-else

Matthias Neubauer neubauer at informatik.uni-freiburg.de
Tue Nov 22 16:33:02 EST 2005


Joel Reymont <joelr1 at gmail.com> writes:

> I don't think it will work for me either way as I'm returning m
> (Maybe Int) where m is my own monad. It seems that folks assumed that
> m itself was the maybe monad. Unless I'm mistaken the code below
> won't work otherwise.

There are two monads involved. The outer return injects into your m
monad. That's all there is for your m.

Then there is the inner stuff. Because the constructor of the inner
expressions, your Maybes, is an instance of MonadPlus, you can use all
the nice stuff there is for MonadPlus.

I'd usually write it like this ...

  return $ do
    guard (cmdType cmd /= CmdSitError Serv) 
    return seat_num

In case the guard fails, you'll get back mzero (Nothing in your
case).

And then there is also mplus to handle alternatives ...

-Matthias


> On Nov 22, 2005, at 8:50 PM, Tomasz Zielonka wrote:
>
>> On Tue, Nov 22, 2005 at 10:15:15PM +0300, Bulat Ziganshin wrote:
>
>>> return $ when (cmdType cmd /= CmdSitError Serv) (return seat_num)
>>>
>>> must also work :)
>>
>> But it won't.
>> I have made this mistake too in the past ;-)
>
> --
> http://wagerlabs.com/
>
>
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>

-- 
Matthias Neubauer                                       |
Universität Freiburg, Institut für Informatik           | tel +49 761 203 8060
Georges-Köhler-Allee 79, 79110 Freiburg i. Br., Germany | fax +49 761 203 8052


More information about the Haskell-Cafe mailing list