September 2017
Beginner to intermediate
396 pages
9h 46m
English
The data type Maybe is defined as follows:
data Maybe a = Just a | Nothing
This is a sum type, which either represents a value, using the Just data constructor, or Nothing. The various functions such as isJust, isNothing, fromJust, listToMaybe, and maybeToList are used to check or extract the values from Maybe.
The interesting function to note is safeOperation. This illustrates the main usage of Maybe. The Maybe value Nothing denotes a failure of some kind in an operation. As a result, all the remaining operations should produce Nothing. This saves us the effort of checking the result of an operation at every step. The safe operation signature is shown here:
safeOperation :: Num a => (a -> a -> Bool) -> (a -> a -> a) -> ...
Read now
Unlock full access