Shortening if-then-else

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Shortening if-then-else

Joel Reymont
Is there a shorter way to write the if-then-else part below?

--
tryTakeSeat :: [Word8] -> Word8 -> ScriptState (Maybe Word8)
tryTakeSeat _ _ =
     do ...
        if (cmdType cmd) /= (CmdSitError Server)
           then return $ Just seat_num
           else return Nothing
--

        Thanks, Joel

--
http://wagerlabs.com/





_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Shortening if-then-else

Arjan van IJzendoorn
> Is there a shorter way to write the if-then-else part below?
>        if (cmdType cmd) /= (CmdSitError Server)
>           then return $ Just seat_num
>           else return Nothing

return $ if cmdType cmd /= CmdSitError Serv
                then Just seat_num else Nothing

Arjan

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Shortening if-then-else

Matthias Neubauer
Arjan van IJzendoorn <[hidden email]> writes:

>> Is there a shorter way to write the if-then-else part below?
>>        if (cmdType cmd) /= (CmdSitError Server)
>>           then return $ Just seat_num
>>           else return Nothing
>
> return $ if cmdType cmd /= CmdSitError Serv
> then Just seat_num else Nothing

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

-Matthias

--
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
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Shortening if-then-else

Henning Thielemann

On Tue, 22 Nov 2005, Matthias Neubauer wrote:

> Arjan van IJzendoorn <[hidden email]> writes:
>
>>> Is there a shorter way to write the if-then-else part below?
>>>        if (cmdType cmd) /= (CmdSitError Server)
>>>           then return $ Just seat_num
>>>           else return Nothing
>>
>> return $ if cmdType cmd /= CmdSitError Serv
>> then Just seat_num else Nothing
>
> return $ guard (cmdType cmd /= CmdSitError Serv) >> return seat_num

Because I often need it, I'm used to use my private function 'toMaybe'

return $ toMaybe (cmdType cmd /= CmdSitError Serv) seat_num
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re[2]: Shortening if-then-else

Bulat Ziganshin
In reply to this post by Matthias Neubauer
Hello Matthias,

Tuesday, November 22, 2005, 9:17:57 PM, you wrote:

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

return $ when (cmdType cmd /= CmdSitError Serv) (return seat_num)

must also work :)


--
Best regards,
 Bulat                            mailto:[hidden email]



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Shortening if-then-else

Tomasz Zielonka
On Tue, Nov 22, 2005 at 10:15:15PM +0300, Bulat Ziganshin wrote:
> Tuesday, November 22, 2005, 9:17:57 PM, you wrote:
>
> MN> return $ guard (cmdType cmd /= CmdSitError Serv) >> return seat_num
>
> 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 ;-)

Best regards
Tomasz
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Shortening if-then-else

Joel Reymont
Why wouldn't Bulat's version work?

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.

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
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Shortening if-then-else

Matthias Neubauer
In reply to this post by Bulat Ziganshin
Bulat Ziganshin <[hidden email]> writes:

> Hello Matthias,
>
> Tuesday, November 22, 2005, 9:17:57 PM, you wrote:
>
> MN> return $ guard (cmdType cmd /= CmdSitError Serv) >> return seat_num
>
> return $ when (cmdType cmd /= CmdSitError Serv) (return seat_num)
>
> must also work :)

Only if seat_num is of type () ... :-)

-Matthias

--
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
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Shortening if-then-else

Yitzchak Gale
In reply to this post by Joel Reymont
> Is there a shorter way to write the if-then-else part below?
>
> --
> tryTakeSeat :: [Word8] -> Word8 -> ScriptState (Maybe Word8)
> tryTakeSeat _ _ =
>     do ...
>        if (cmdType cmd) /= (CmdSitError Server)
>           then return $ Just seat_num
>           else return Nothing
> --

tryTakeSeat _ _ = runMaybeT $ do
       ...
       guard $ cmdType cmd /= CmdSitError Server
       return seat_num

-Yitz
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Shortening if-then-else

Matthias Neubauer
In reply to this post by Joel Reymont
Joel Reymont <[hidden email]> 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
> [hidden email]
> 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
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Shortening if-then-else

Tomasz Zielonka
In reply to this post by Joel Reymont
On Tue, Nov 22, 2005 at 09:07:29PM +0000, Joel Reymont wrote:
> Why wouldn't Bulat's version work?

Because Int /= ()

    when :: (Monad m) => Bool -> m () -> m ()

Best regards
Tomasz
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe