Merge multiple cases in Haskell

2

With a case _ of syntax like this:

fun a b c =
    case (a, b, c) of
        (Just True, Just _, _) -> foo
        (Just True, _, Just _) -> foo
        _                      -> bar

Can I merge the first two conditions and avoid repeating foo?

Alternatively, is there any other (cleaner) way to express that I want to run foo if and only if a is Just True and either b or c are not Nothing?

Share
Improve this question
5
  • Could you set a d = b or c right before the case, and then do a case (a, d) of (Just True, not Nothing) -> foo? – rturrado Apr 29 at 22:58
  • 1
    @rturrado Maybe values don't work like that with or or not. – chepner Apr 29 at 23:02
  • @chepner OK, thanks; it was pseudo code. I haven't programmed in Haskell for a while. – rturrado Apr 29 at 23:11
  • 1
    @rturrado You are morally right: Daniel Wagner's answer below essentially follows your approach. – chi Apr 30 at 7:14
  • @chi Haha, thanks! I somehow helped then ;-) – rturrado Apr 30 at 15:32

Comments

Popular posts from this blog

Meaning of `{}` for return expression

Get current scroll position of ScrollView in React Native

flutter websocket connection issue