[Haskell-beginners] Value from Monad

Brandon Allbery allbery.b at gmail.com
Wed Apr 22 15:43:38 UTC 2015


On Wed, Apr 22, 2015 at 11:38 AM, Shishir Srivastava <
shishir.srivastava at gmail.com> wrote:

> My next question was how do i only get "34" or "3" i.e. the Maybe value
> without wrapped in to a 'Maybe' ?


"do" syntax lets you temporarily "unwrap" the value, but it must be
rewrapped later. And if the specific monad in question is not IO, you
cannot do I/O (e.g. "print" the value as in your initial message). This
reflects the mapping of "do" to uses of (>>=)

    Prelude> :t (>>=)
    (>>=) :: Monad m => m a -> (a -> m b) -> m b

In this example, `m` is Maybe.

As it turns out, Maybe is one of the monads that lets you use pattern
matching to extract values. Not all monads do; you cannot do this with IO,
for example. The `maybe` function is often used instead of explicit pattern
matching.

-- 
brandon s allbery kf8nh                               sine nomine associates
allbery.b at gmail.com                                  ballbery at sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150422/60760bc3/attachment.html>


More information about the Beginners mailing list