
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 [To: haskell-cafe@haskell.org] Is there a nicer way of writing the following sort of code? case (number g) of Just n -> Just (show n) Nothing -> case (fraction g) of Just n -> Just (show n) Nothing -> case (nimber g) of Just n -> Just ("*"++(show n)) Nothing -> Nothing - -- Russell O'Connor roconnor@math.berkeley.edu http://www.math.berkeley.edu/~roconnor/ ``Any time you skip a commercial [...] you're actually stealing the programming'' -- Jamie Kellner, CEO of TBS -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.6 (SunOS) Comment: For info see http://www.gnupg.org iD8DBQE9jM7juZUa0PWVyWQRAjmlAJ9+YDXUEXEhLTtb2huBZKskJ9JvegCgjPb2 iGb/vYYLK7I+UdfijtEePyQ= =6UZh -----END PGP SIGNATURE-----

On Sat, Sep 21, 2002 at 12:56:13PM -0700, Russell O'Connor wrote:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
[To: haskell-cafe@haskell.org]
Is there a nicer way of writing the following sort of code?
case (number g) of Just n -> Just (show n) Nothing -> case (fraction g) of Just n -> Just (show n) Nothing -> case (nimber g) of Just n -> Just ("*"++(show n)) Nothing -> Nothing
You could write (using GHC's pattern guards): show g | Just n = number g = Just (show n) | Just n = fraction g = Just (show n) | Just n = nimber g = Just ("*"++show n) | Nothing = Nothing Do I detect a program for analyzing combinatorial games being written? --Dylan

"Russell O'Connor"
[To: haskell-cafe@haskell.org]
Is there a nicer way of writing the following sort of code?
case (number g) of Just n -> Just (show n) Nothing -> case (fraction g) of Just n -> Just (show n) Nothing -> case (nimber g) of Just n -> Just ("*"++(show n)) Nothing -> Nothing
what about? listToMaybe $ mapMaybe (\ (f, format) -> fmap format (f g)) l where l = [ (number, show), (fraction, show), (nimber, (\ n -> "*" ++ show n)) ] or: if_just (number g) show $ if_just (fraction g) show $ if_just (nimber g) (\ n -> "*" ++ show n) Nothing using: if_just (Just e) f _ = Just(f e) if_just Nothing _ f = f

On 2002-09-21T12:56:13-0700, Russell O'Connor wrote:
case (number g) of Just n -> Just (show n) Nothing -> case (fraction g) of Just n -> Just (show n) Nothing -> case (nimber g) of Just n -> Just ("*"++(show n)) Nothing -> Nothing
How about something like: msum [ liftM show (number g) , liftM show (fraction g) , liftM (("*"++).show) (nimber g) ] -- Edit this signature at http://www.digitas.harvard.edu/cgi-bin/ken/sig So the choice which Lomborg presents us with, of whether to save a drowning Tuvaluan (climate change) or a dying Somalian (water and sanitation) is not a choice at all -- in fact we need to do both, and not least because one is unlikely to be successful without the other. http://www.anti-lomborg.com/

G'day all. On Sat, Sep 21, 2002 at 12:56:13PM -0700, Russell O'Connor wrote:
case (number g) of Just n -> Just (show n) Nothing -> case (fraction g) of Just n -> Just (show n) Nothing -> case (nimber g) of Just n -> Just ("*"++(show n)) Nothing -> Nothing
This isn't exactly the most beautiful way of doing it, but... (number g >>= return . show) `mplus` (fraction g >>= return . show) `mplus` (nimber g >>= return . ('*':) . show) Cheers, Andrew Bromage

I know this has been written about way too much, but I was wondering what people thought about using 'liftM f' as opposed to '>>= return . f'. I would probably have written Andrew's code using liftM, but I don't know if one is necessarily better than the other. Does anyone have strong thoughts on this? -- Hal Daume III "Computer science is no more about computers | hdaume@isi.edu than astronomy is about telescopes." -Dijkstra | www.isi.edu/~hdaume On Sun, 22 Sep 2002, Andrew J Bromage wrote:
G'day all.
On Sat, Sep 21, 2002 at 12:56:13PM -0700, Russell O'Connor wrote:
case (number g) of Just n -> Just (show n) Nothing -> case (fraction g) of Just n -> Just (show n) Nothing -> case (nimber g) of Just n -> Just ("*"++(show n)) Nothing -> Nothing
This isn't exactly the most beautiful way of doing it, but...
(number g >>= return . show) `mplus` (fraction g >>= return . show) `mplus` (nimber g >>= return . ('*':) . show)
Cheers, Andrew Bromage _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

hi, i personally think that one should use fmap instead. it tells you exactly how much structure you need. having said that it is sometimes annoying to get the extra Functor constraint (alas Functor is not a super class of Moand)... so my preference is: 1. fmap 2. liftM 3. bind with return bye iavor On Mon, Sep 23, 2002 at 02:57:45PM -0700, Hal Daume III wrote:
I know this has been written about way too much, but I was wondering what people thought about using 'liftM f' as opposed to '>>= return . f'. I would probably have written Andrew's code using liftM, but I don't know if one is necessarily better than the other. Does anyone have strong thoughts on this?
-- Hal Daume III
"Computer science is no more about computers | hdaume@isi.edu than astronomy is about telescopes." -Dijkstra | www.isi.edu/~hdaume
On Sun, 22 Sep 2002, Andrew J Bromage wrote:
G'day all.
On Sat, Sep 21, 2002 at 12:56:13PM -0700, Russell O'Connor wrote:
case (number g) of Just n -> Just (show n) Nothing -> case (fraction g) of Just n -> Just (show n) Nothing -> case (nimber g) of Just n -> Just ("*"++(show n)) Nothing -> Nothing
This isn't exactly the most beautiful way of doing it, but...
(number g >>= return . show) `mplus` (fraction g >>= return . show) `mplus` (nimber g >>= return . ('*':) . show)
Cheers, Andrew Bromage _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- ---------------------------------------+---------------------------------------- Iavor S. Diatchki | email: diatchki@cse.ogi.edu Dept. of Computer Science | web: http://www.cse.ogi.edu/~diatchki OGI School of Science and Engineering | work phone: 5037481631 OHSU | home phone: 5036434085 ---------------------------------------+----------------------------------------

Hal Daume
I know this has been written about way too much, but I was wondering what people thought about using 'liftM f' as opposed to '>>= return . f'. I would probably have written Andrew's code using liftM, but I don't know if one is necessarily better than the other. Does anyone have strong thoughts on this?
I tend to use liftM only as part of a pattern of lifting a family of non-monadic functions up to the monadic level in code like this: instance (Monad m, Num a) => Num (m a) where (+) = liftM2 (+) (-) = liftM2 (-) negate = liftM1 negate ... instance (Monad m, Integral a) => Integral (m a) where ... If it doesn't feel like lifting and/or it isn't part of a pattern like the above, I tend not to use it. A
participants (8)
-
Alastair Reid
-
Andrew J Bromage
-
Dylan Thurston
-
Hal Daume III
-
Iavor Diatchki
-
Ken Shan
-
Pixel
-
Russell O'Connor