Shortening if-then-else

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/

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
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

On Tue, 22 Nov 2005, Matthias Neubauer wrote:
Arjan van IJzendoorn
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

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:bulatz@HotPOP.com

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

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 ;-)

Joel Reymont
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 ;-)
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@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

Bulat Ziganshin
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

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
participants (7)
-
Arjan van IJzendoorn
-
Bulat Ziganshin
-
Henning Thielemann
-
Joel Reymont
-
Matthias Neubauer
-
Tomasz Zielonka
-
Yitzchak Gale