[Haskell-cafe] Shortening if-then-else
Arjan van IJzendoorn wrote: | 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 There's a subtle change in semantics when we move the 'if' inside the 'return'. The original code requires the condition to be evaluated as part of the do-expression's monad's structure, but the translated code defers it. 'return $! if ...' would be closer to the original. Regards, Tom ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] 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/ ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Shortening if-then-else
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 Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Shortening if-then-else
Arjan van IJzendoorn [EMAIL PROTECTED] 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 Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Shortening if-then-else
On Tue, 22 Nov 2005, Matthias Neubauer wrote: Arjan van IJzendoorn [EMAIL PROTECTED] 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 Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re[2]: [Haskell-cafe] Shortening if-then-else
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, Bulatmailto:[EMAIL PROTECTED] ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Shortening if-then-else
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 Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Shortening if-then-else
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 Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Shortening if-then-else
Bulat Ziganshin [EMAIL PROTECTED] 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 Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] 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 -- tryTakeSeat _ _ = runMaybeT $ do ... guard $ cmdType cmd /= CmdSitError Server return seat_num -Yitz ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Shortening if-then-else
Joel Reymont [EMAIL PROTECTED] 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@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 ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Shortening if-then-else
On Tue, Nov 22, 2005 at 09:07:29PM +, 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 Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe