[Haskell-beginners] combine expressions in do block

Brent Yorgey byorgey at seas.upenn.edu
Wed Jan 8 20:31:05 UTC 2014


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


More information about the Beginners mailing list