Difference between revisions of "Maybe"

From HaskellWiki
Jump to navigation Jump to search
m (→‎Type Equation: - Fixing mistake in type eqn - thanks Mark)
Line 47: Line 47:
 
==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:
;<code>maybe:: b->(a->b) -> Maybe a -> b</code> :Applies the second argument to the third, when it is '''Just x'', otherwise returns the first argument.
+
;<code>maybe:: b->(a->b) -> Maybe a -> b</code> :Applies the second argument to the third, when it is ''Just x'', otherwise returns the first argument.
 
;<code>isJust, isNothing</code> :Test the argument, returing a Bool based on the constructor.
 
;<code>isJust, isNothing</code> :Test the argument, returing a Bool based on the constructor.
 
;<code>listToMaybe, maybeToList</code> :Convert to/from a one element or empty list.
 
;<code>listToMaybe, maybeToList</code> :Convert to/from a one element or empty list.

Revision as of 04:07, 7 March 2006

Maybe class (base)
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.

Type Equation

Maybe satisfies the type equation , where the functor takes a set to a point plus that set.

Comparison to imperative languages

Most imperative languages will ignore this, or allow one to use NULL (defined in some manner) to specify a value might not be there.

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.

Usage example

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

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 explatation and other functions.