# Maybe

(Difference between revisions)
 Revision as of 15:46, 7 October 2006 (edit)m (Categorizing as a monad)← Previous diff Current revision (13:53, 30 April 2012) (edit) (undo) (removed redundant nowiki tag) (One intermediate revision not shown.) Line 7: Line 7: It allows the programmer to specify something may not be there. It allows the programmer to specify something may not be there. - ==Type Equation== + ==Type equation== '''Maybe''' satisfies the [[type]] equation [itex]F X = 1 + X[/itex], where the functor [itex]F[/itex] takes a set to a point plus that set. '''Maybe''' satisfies the [[type]] equation [itex]F X = 1 + X[/itex], where the functor [itex]F[/itex] takes a set to a point plus that set. Line 45: Line 45: The functions h and h' will give the same results. ([itex]h 0 = h' 0 = h 100 = h' 100 = Nothing;\ h x = h' x = Just\, x[/itex]). In this case the savings in code size is quite modest, stringing together multiple functions like f and g will be more noticeable. The functions h and h' will give the same results. ([itex]h 0 = h' 0 = h 100 = h' 100 = Nothing;\ h x = h' x = Just\, x[/itex]). In this case the savings in code size is quite modest, stringing together multiple functions like f and g will be more noticeable. - ==Library Functions== + ==Library functions== When the module is imported, it supplies a variety of useful functions including: When the module is imported, it supplies a variety of useful functions including: - ;maybe :: b->(a->b) -> Maybe a -> b :Applies the second argument to the third, when it is ''Just x'', otherwise returns the first argument. + ;maybe :: b->(a->b) -> Maybe a -> b :Applies the second argument to the third, when it is ''Just x'', otherwise returns the first argument. ;isJust, isNothing :Test the argument, returing a Bool based on the constructor. ;isJust, isNothing :Test the argument, returing a Bool based on the constructor. ;listToMaybe, maybeToList :Convert to/from a one element or empty list. ;listToMaybe, maybeToList :Convert to/from a one element or empty list.

## Current revision

 import Data.Maybe

The Maybe type is defined as follows:

data Maybe a = Just a | Nothing
deriving (Eq, Ord)

It allows the programmer to specify something may not be there.

## 1 Type equation

Maybe satisfies the type equation FX = 1 + X, where the functor F takes a set to a point plus that set.

## 2 Comparison to imperative languages

Imperative languages may support this by rewriting as a union or allow one to use / return NULL (defined in some manner) to specify a value might not be there.

## 3 Classes

As one can see from the type definition, Maybe will be an instance of Eq and Ord when the base type is. As well, instances of Functor and Monad are defined for Maybe.

For Functor, the fmap function moves inside the Just constructor and is identity on the Nothing constructor.

For Monad, the bind operation passes through Just, while Nothing will force the result to always be Nothing.

### 3.1 Maybe as a Monad

Using the Monad class definition can lead to much more compact code. For example:

f::Int -> Maybe Int
f 0 = Nothing
f x = Just x

g :: Int -> Maybe Int
g 100 = Nothing
g x = Just x

h ::Int -> Maybe Int
h x = case f x of
Just n -> g n
Nothing -> Nothing

h' :: Int -> Maybe Int
h' x = do n <- f x
g n

The functions h and h' will give the same results. ($h 0 = h' 0 = h 100 = h' 100 = Nothing;\ h x = h' x = Just\, x$). In this case the savings in code size is quite modest, stringing together multiple functions like f and g will be more noticeable.

## 4 Library functions

When the module is imported, it supplies a variety of useful functions including:

maybe :: b->(a->b) -> Maybe a -> b

Applies the second argument to the third, when it is Just x, otherwise returns the first argument.
isJust
,
isNothing

Test the argument, returing a Bool based on the constructor.
listToMaybe
,
maybeToList

Convert to/from a one element or empty list.
mapMaybe

A different way to filter a list.

See the documentation for Data.Maybe for more explanation and other functions.