Send Beginners mailing list submissions to
        beginners@haskell.org

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
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

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


Today's Topics:

   1.  monad question (mike h)
   2. Re:  monad question (David McBride)
   3. Re:  monad question (mike h)
   4. Re:  monad question (David McBride)


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

Message: 1
Date: Fri, 13 Oct 2017 19:15:23 +0100
From: mike h <mike_k_hough...@yahoo.co.uk>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: [Haskell-beginners] monad question
Message-ID: <513b83f7-b469-4967-8deb-9cf281e6d...@yahoo.co.uk>
Content-Type: text/plain; charset=utf-8


I have 

cap :: String -> String
cap = toUpper

rev :: String -> String
rev = reverse

then I make 

tupled :: String -> (String, String)
tupled = do
    r <- rev
    c <- cap
    return (r, c)

and to be honest, yes it’s been a long day at work, and this is coding at home 
rather than coding (java) at work but
I’m not sure how tupled  works!!!
My first shot was supplying a param s like this

tupled :: String -> (String, String)
tupled s = do
    r <- rev s
    c <- cap s
    return (r, c)

which doesn’t compile. But how does the first version work? How does the string 
to be processed get into the rev and cap functions??

Thanks

Mike






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

Message: 2
Date: Fri, 13 Oct 2017 14:35:22 -0400
From: David McBride <toa...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] monad question
Message-ID:
        <CAN+Tr43iPHWoUv+nXnr15KbB+M7T4eSuKFAix=ha22jxtiv...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Functions are Monads.

:i Monad
class Applicative m => Monad (m :: * -> *) where
  (>>=) :: m a -> (a -> m b) -> m b
  (>>) :: m a -> m b -> m b
  return :: a -> m a
...
instance Monad (Either e) -- Defined in ‘Data.Either’
instance Monad [] -- Defined in ‘GHC.Base’
...
instance Monad ((->) r) -- Defined in ‘GHC.Base’

That last instance means if I have a function whose first argument is type
r, that is a monad.  And if you fill in the types of the various monad
functions you would get something like this

(>>=) :: ((->) r) a -> (a -> ((-> r) b) -> ((-> r) b)
(>>=) :: (r -> a) -> (a -> (r -> b)) -> (r -> b) -- simplified
return :: a -> (r -> a)

So in the same way that (IO String) is a Monad and can use do notation, (a
-> String) is also a Monad, and can also use do notation.  Hopefully that
made sense.

On Fri, Oct 13, 2017 at 2:15 PM, mike h <mike_k_hough...@yahoo.co.uk> wrote:

>
> I have
>
> cap :: String -> String
> cap = toUpper
>
> rev :: String -> String
> rev = reverse
>
> then I make
>
> tupled :: String -> (String, String)
> tupled = do
>     r <- rev
>     c <- cap
>     return (r, c)
>
> and to be honest, yes it’s been a long day at work, and this is coding at
> home rather than coding (java) at work but
> I’m not sure how tupled  works!!!
> My first shot was supplying a param s like this
>
> tupled :: String -> (String, String)
> tupled s = do
>     r <- rev s
>     c <- cap s
>     return (r, c)
>
> which doesn’t compile. But how does the first version work? How does the
> string to be processed get into the rev and cap functions??
>
> Thanks
>
> Mike
>
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20171013/80941fa7/attachment-0001.html>

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

Message: 3
Date: Fri, 13 Oct 2017 20:05:28 +0100
From: mike h <mike_k_hough...@yahoo.co.uk>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] monad question
Message-ID: <04d773af-e78b-494d-9fd6-f81ebde71...@yahoo.co.uk>
Content-Type: text/plain; charset="utf-8"

That certainly helps me  David, thanks.
How then would you write
> tupled :: String -> (String, String)

 
with the parameter written explicitly? i.e.

tupled s = do …

or does the question not make sense in light of your earlier reply?

Thanks

Mike




> On 13 Oct 2017, at 19:35, David McBride <toa...@gmail.com> wrote:
> 
> Functions are Monads.
> 
> :i Monad
> class Applicative m => Monad (m :: * -> *) where
>   (>>=) :: m a -> (a -> m b) -> m b
>   (>>) :: m a -> m b -> m b
>   return :: a -> m a
> ...
> instance Monad (Either e) -- Defined in ‘Data.Either’
> instance Monad [] -- Defined in ‘GHC.Base’
> ...
> instance Monad ((->) r) -- Defined in ‘GHC.Base’
> 
> That last instance means if I have a function whose first argument is type r, 
> that is a monad.  And if you fill in the types of the various monad functions 
> you would get something like this
> 
> (>>=) :: ((->) r) a -> (a -> ((-> r) b) -> ((-> r) b)
> (>>=) :: (r -> a) -> (a -> (r -> b)) -> (r -> b) -- simplified
> return :: a -> (r -> a)
> 
> So in the same way that (IO String) is a Monad and can use do notation, (a -> 
> String) is also a Monad, and can also use do notation.  Hopefully that made 
> sense.
> 
> On Fri, Oct 13, 2017 at 2:15 PM, mike h <mike_k_hough...@yahoo.co.uk 
> <mailto:mike_k_hough...@yahoo.co.uk>> wrote:
> 
> I have
> 
> cap :: String -> String
> cap = toUpper
> 
> rev :: String -> String
> rev = reverse
> 
> then I make
> 
> tupled :: String -> (String, String)
> tupled = do
>     r <- rev
>     c <- cap
>     return (r, c)
> 
> and to be honest, yes it’s been a long day at work, and this is coding at 
> home rather than coding (java) at work but
> I’m not sure how tupled  works!!!
> My first shot was supplying a param s like this
> 
> tupled :: String -> (String, String)
> tupled s = do
>     r <- rev s
>     c <- cap s
>     return (r, c)
> 
> which doesn’t compile. But how does the first version work? How does the 
> string to be processed get into the rev and cap functions??
> 
> Thanks
> 
> Mike
> 
> 
> 
> 
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org <mailto:Beginners@haskell.org>
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners 
> <http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners>
> 
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20171013/8024e7fd/attachment-0001.html>

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

Message: 4
Date: Fri, 13 Oct 2017 15:13:31 -0400
From: David McBride <toa...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] monad question
Message-ID:
        <CAN+Tr42srd7dmv7d_1FmiSXMY4g+Cd=yuqww8b2-sdpzdu7...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

If you are using do notation, you can't.  If you aren't you can write

tupled s = (rev s, cap s)

Your old tupled is equivalent to this

tupled = rev >>= \s -> cap >>= \c -> return (s, c)

which is quite different.

On Fri, Oct 13, 2017 at 3:05 PM, mike h <mike_k_hough...@yahoo.co.uk> wrote:

> That certainly helps me  David, thanks.
> How then would you write
>
> tupled :: String -> (String, String)
>
>
>
> with the parameter written explicitly? i.e.
>
> tupled s = do …
>
> or does the question not make sense in light of your earlier reply?
>
> Thanks
>
> Mike
>
>
>
>
> On 13 Oct 2017, at 19:35, David McBride <toa...@gmail.com> wrote:
>
> Functions are Monads.
>
> :i Monad
> class Applicative m => Monad (m :: * -> *) where
>   (>>=) :: m a -> (a -> m b) -> m b
>   (>>) :: m a -> m b -> m b
>   return :: a -> m a
> ...
> instance Monad (Either e) -- Defined in ‘Data.Either’
> instance Monad [] -- Defined in ‘GHC.Base’
> ...
> instance Monad ((->) r) -- Defined in ‘GHC.Base’
>
> That last instance means if I have a function whose first argument is type
> r, that is a monad.  And if you fill in the types of the various monad
> functions you would get something like this
>
> (>>=) :: ((->) r) a -> (a -> ((-> r) b) -> ((-> r) b)
> (>>=) :: (r -> a) -> (a -> (r -> b)) -> (r -> b) -- simplified
> return :: a -> (r -> a)
>
> So in the same way that (IO String) is a Monad and can use do notation, (a
> -> String) is also a Monad, and can also use do notation.  Hopefully that
> made sense.
>
> On Fri, Oct 13, 2017 at 2:15 PM, mike h <mike_k_hough...@yahoo.co.uk>
> wrote:
>
>>
>> I have
>>
>> cap :: String -> String
>> cap = toUpper
>>
>> rev :: String -> String
>> rev = reverse
>>
>> then I make
>>
>> tupled :: String -> (String, String)
>> tupled = do
>>     r <- rev
>>     c <- cap
>>     return (r, c)
>>
>> and to be honest, yes it’s been a long day at work, and this is coding at
>> home rather than coding (java) at work but
>> I’m not sure how tupled  works!!!
>> My first shot was supplying a param s like this
>>
>> tupled :: String -> (String, String)
>> tupled s = do
>>     r <- rev s
>>     c <- cap s
>>     return (r, c)
>>
>> which doesn’t compile. But how does the first version work? How does the
>> string to be processed get into the rev and cap functions??
>>
>> Thanks
>>
>> Mike
>>
>>
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> Beginners@haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20171013/1a5cdaf5/attachment.html>

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

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


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

End of Beginners Digest, Vol 112, Issue 13
******************************************

Reply via email to