Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  Value from Monad (Shishir Srivastava)
   2. Re:  Value from Monad
      (Sumit Sahrawat, Maths & Computing, IIT (BHU))
   3. Re:  Value from Monad (Brandon Allbery)
   4. Re:  Value from Monad (Shishir Srivastava)
   5. Re:  Value from Monad (Mike Meyer)
   6. Re:  Value from Monad (Brandon Allbery)
   7. Re:  Value from Monad (Marcin Mrotek)


----------------------------------------------------------------------

Message: 1
Date: Wed, 22 Apr 2015 16:24:23 +0100
From: Shishir Srivastava <[email protected]>
To: beginners <[email protected]>
Subject: [Haskell-beginners] Value from Monad
Message-ID:
        <cale5rtu2watosu4vstkz+5pnmyfkujbwdmcrsbbg-sedmzn...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Hi,

Going through the '<-' description in 'learnouahaskell' it says -

*" If we have a Maybe String and we bind it with <- to a variable, that
variable will be a String"*

so if you write

do
str <- Just "3"

str should be of type 'String'. My question is how do I return "34"
by concatenating it to "4" because the following line of code fails -

---
do
str <- Just "3"
return str++"4"::Maybe String
---

or for that matter how can I return/print just the value of str from this
do block ?

Thanks,
Shishir
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150422/079b28e7/attachment-0001.html>

------------------------------

Message: 2
Date: Wed, 22 Apr 2015 20:55:45 +0530
From: "Sumit Sahrawat, Maths & Computing, IIT (BHU)"
        <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Value from Monad
Message-ID:
        <CAJbEW8MAGON0543rK8MAYKTw9PW0Omb=x8s2fashwwoaszd...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

return (str++"4") should work

On 22 April 2015 at 20:54, Shishir Srivastava <[email protected]>
wrote:

> Hi,
>
> Going through the '<-' description in 'learnouahaskell' it says -
>
> *" If we have a Maybe String and we bind it with <- to a variable, that
> variable will be a String"*
>
> so if you write
>
> do
> str <- Just "3"
>
> str should be of type 'String'. My question is how do I return "34"
> by concatenating it to "4" because the following line of code fails -
>
> ---
> do
> str <- Just "3"
> return str++"4"::Maybe String
> ---
>
> or for that matter how can I return/print just the value of str from this
> do block ?
>
> Thanks,
> Shishir
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>


-- 
Regards

Sumit Sahrawat
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150422/f0da6b66/attachment-0001.html>

------------------------------

Message: 3
Date: Wed, 22 Apr 2015 11:28:16 -0400
From: Brandon Allbery <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Value from Monad
Message-ID:
        <CAKFCL4XoCif5_hW_+C+Q3gtzaf=iS=pnzljhm+olvntretw...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Wed, Apr 22, 2015 at 11:24 AM, Shishir Srivastava <
[email protected]> wrote:

> return str++"4"::Maybe String
>

This is parsed as "(return str) ++ "4". Function application is always
higher precedence than operators.

-- 
brandon s allbery kf8nh                               sine nomine associates
[email protected]                                  [email protected]
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150422/a89d23ab/attachment-0001.html>

------------------------------

Message: 4
Date: Wed, 22 Apr 2015 16:38:34 +0100
From: Shishir Srivastava <[email protected]>
To: beginners <[email protected]>
Subject: Re: [Haskell-beginners] Value from Monad
Message-ID:
        <cale5rts7l5ugcspckona4-k7p-p0n5eryd4zrfs14f0rfic...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150422/801962ab/attachment-0001.html>

------------------------------

Message: 5
Date: Wed, 22 Apr 2015 10:41:15 -0500
From: Mike Meyer <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Value from Monad
Message-ID:
        <CAD=7u2bhoj+86gdb1mdwnryzqgx4kexdft3xc3zpneye-r6...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

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 <
[email protected]> 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
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150422/cb813ea5/attachment-0001.html>

------------------------------

Message: 6
Date: Wed, 22 Apr 2015 11:43:38 -0400
From: Brandon Allbery <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Value from Monad
Message-ID:
        <cakfcl4veevg9n0iqxdezjhvyxt8ryqvuyp3y6hckmrjd8nq...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Wed, Apr 22, 2015 at 11:38 AM, Shishir Srivastava <
[email protected]> 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
[email protected]                                  [email protected]
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-0001.html>

------------------------------

Message: 7
Date: Wed, 22 Apr 2015 22:23:36 +0200
From: Marcin Mrotek <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Value from Monad
Message-ID:
        <cajcfpzkbk6pcuyalaepu5_7ksufwtw5fjanffi9o7updnyf...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

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


------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 82, Issue 30
*****************************************

Reply via email to