I was playing with the Maybe type and realised that there are useful functions provided to play with these types.
Then I realised that in my definition of ternary operator in haskell the definition of (!)
> Nothing ! a = a
> Just b ! _ = b
was very similar to the predefined function fromMaybe (only with its arguments reversed)
In fact, we can define the ternary operator using functions defined in Data.Maybe!
> (?) = boolToMaybe
> (!) = flip fromMaybe
Ok ok, that's not quite true. boolToMaybe doesn't exist in Data.Maybe, but the similar listToMaybe does.
We'd have to define
> boolToMaybe True a = Just a
> boolToMaybe False _ = Nothing
Or, a cute but less clear version (that makes clear the heritage with listToMaybe):
> boolToMaybe b v = listToMaybe $ filter (const b) [v]