The
fromMaybe function takes a default value and a
Maybe
value. If the
Maybe is
Nothing, it returns the default
value; otherwise, it returns the value contained in the
Maybe.
Examples
Basic usage:
>>> fromMaybe "" (Just "Hello, World!")
"Hello, World!"
>>> fromMaybe "" Nothing
""
Read an integer from a string using
readMaybe. If we fail to
parse an integer, we want to return
0 by default:
>>> import GHC.Internal.Text.Read ( readMaybe )
>>> fromMaybe 0 (readMaybe "5")
5
>>> fromMaybe 0 (readMaybe "")
0