
Is it possible to bind two expressions together within a case inside a do block? Given the following code running in the writer monad: f a = do case g a of Nothing -> return () Just b -> h a (field1 b) (field2 b) case g a of Nothing -> return () Just b -> i a (j a) (field1 b) (field2 b) How can I combine these two cases together (or otherwise simplify the code)? Thanks, Renah Scarowsky Suite Solutions Create>Manage>Deploy http://www.suite-sol.com/ http://www.suite-sol.com

Hi Renah, On Wed, Jan 08, 2014 at 10:09:18AM +0200, Renah Scarowsky wrote:
How can I combine these two cases together (or otherwise simplify the code)?
I don't know if I'm understanding you correctly, but something like this? f a = do case g a of Nothing -> return () Just b -> do h a (field1 b) (field2 b) i a (j a) (field1 b) (field2 b) Greetings, Daniel

On Wed, Jan 08, 2014 at 09:21:59AM +0100, Daniel Trstenjak wrote:
Hi Renah,
On Wed, Jan 08, 2014 at 10:09:18AM +0200, Renah Scarowsky wrote:
How can I combine these two cases together (or otherwise simplify the code)?
I don't know if I'm understanding you correctly, but something like this?
f a = do case g a of Nothing -> return () Just b -> do h a (field1 b) (field2 b) i a (j a) (field1 b) (field2 b)
To take this a step further, if 'field1' and 'field2' are projections, then you might be able to replace them with pattern-matching as well: f a = do case g a of Nothing -> return () Just (Constructor f1 f2) -> do h a f1 f2 i a (j a) f1 f2 I know this is just a made-up example and not real code, but figured it was worth mentioning anyway. -Brent
participants (3)
-
Brent Yorgey
-
Daniel Trstenjak
-
Renah Scarowsky