haskell - Maybe monad usage example -


i'm trying learn haskell i'm having problem in monad usage.

i imported module data.maybe.

but don't know how use >>= operator.

given (>>=) :: monad m => m -> (a -> m b) -> m b cannot understand how define function (a -> m b).

can provide pedagogical example?

for maybe monad bind function (>>=) looks that:

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

so, let's define maybe a value:

> let = 1 :: maybe integer 

and :: -> maybe b function:

> let f = \x -> (x+1) f :: integer -> maybe integer 

now can use bind infix operator:

> >>= f 2 :: maybe integer 

another example of a -> maybe b function be:

let h :: integer -> maybe string; h = return . show . (+1) h :: integer -> maybe string 

so h increment integer number, convert string , make maybe value return function.

> >>= h "2" 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -