O'Reilly logo

Haskell Cookbook by Yogesh Sajanikar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How it works...

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 NothingThis 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) -> ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required