Home > Net >  How can I declare a function with a parameter that can be either (Int, Int) or Maybe (Int, Int)?
How can I declare a function with a parameter that can be either (Int, Int) or Maybe (Int, Int)?

Time:11-06

I am trying to write a function whose third parameter can be an (Int, Int) or a Maybe (Int, Int). Is this possible?

randomFunction :: [Char] -> [[Int]] -> Either (Int, Int) Maybe (Int, Int) -> Bool

CodePudding user response:

The syntax for the thing you ask is:

Either (Int, Int) (Maybe (Int, Int))

But this is unlikely to be the idiomatic solution. I suspect it's much more likely to be one of these two solutions:

  1. Accept only Maybe (Int, Int). Callers that have an (Int, Int) in hand can wrap it in a Just. (This solution can always be applied.)
  2. Accept only (Int, Int). Offer a default (Int, Int) -- say, (0, 0), though of course the right choice depends on what the function does -- that callers can use if they have a Maybe (Int, Int) and its value is Nothing. (This solution is almost always possible, but in rare cases, the function might treat Nothing so specially that it cannot be emulated by any specific (Int, Int).)
  • Related