I'm wondering how I could go about defining an operator / if an operator already exists in Haskell that lets pick the first Maybe
type that isn't Nothing
, and if all if them are nothing, it returns some default. Basically something like this:
let x = a ?? b ?? c ?? 1
where (??)
is the operator and a, b, c
are all Maybe
types. I'd expect this code to return the first value that is Just
or 1
if all of a, b, c
are Nothing
.
I'm basically looking to replicate the same functionality as the null coalescing operator you'd find in C#
or other languages.
This question shows how to do it in F# (Null Coalescing Operator in F#?), and it's quite hacky. Is there a way to do it cleanly in Haskell, and if not, what's the closest you can get to it?