Re: [Haskell-beginners] Value from Monad

Thanks ! that does work now. My next question was how do i only get "34" or "3" i.e. the Maybe value without wrapped in to a 'Maybe' ? Shishir

You should use something like the maybe function to extract it after you've returned the Just value. On Wed, Apr 22, 2015 at 10:38 AM, Shishir Srivastava < shishir.srivastava@gmail.com> wrote:
Thanks ! that does work now.
My next question was how do i only get "34" or "3" i.e. the Maybe value without wrapped in to a 'Maybe' ?
Shishir
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

On Wed, Apr 22, 2015 at 11:38 AM, Shishir Srivastava < shishir.srivastava@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@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

Hello, Well, you CAN use fromJust, but bear in mind that you should only use it if you are absolutely, positively, 100% sure the Maybe value you are calling fromJust on is of the form (Just something), because your program will error out if you call fromJust on a Nothing. Even if you're sure it's not a Nothing, it might be better to use maybe (error "Impossible, because (...)") id instead, so that you at the very least get a meaningful error message in case "impossible" happens ;) Other than that, it's recomennded to either use the maybe function, or pattern matching: case foo of Just x -> ... Nothing -> ... instead. Best regards, Marcin Mrotek

Even if you're sure it's not a Nothing, it might be better to use
maybe (error "Impossible, because (...)") id
Which is the same thing as:
fromMaybe (error "Impossible, because (...)")
On Wed, Apr 22, 2015 at 1:23 PM, Marcin Mrotek
Hello,
Well, you CAN use fromJust, but bear in mind that you should only use it if you are absolutely, positively, 100% sure the Maybe value you are calling fromJust on is of the form (Just something), because your program will error out if you call fromJust on a Nothing. Even if you're sure it's not a Nothing, it might be better to use
maybe (error "Impossible, because (...)") id
instead, so that you at the very least get a meaningful error message in case "impossible" happens ;) Other than that, it's recomennded to either use the maybe function, or pattern matching:
case foo of Just x -> ... Nothing -> ...
instead.
Best regards, Marcin Mrotek _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
participants (5)
-
Alex Hammel
-
Brandon Allbery
-
Marcin Mrotek
-
Mike Meyer
-
Shishir Srivastava