Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/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.  liftM2, how does it work? ([email protected])
   2.  Text.JSON.JSON instance for Data.Text.Text (Vincent Ambo)
   3.  Example code on haskell.org (Joshua Weaver)
   4. Re:  liftM2, how does it work? (Kyle Murphy)
   5. Re:  liftM2, how does it work? (Franco)


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

Message: 1
Date: Fri, 24 Feb 2012 17:42:51 +0100
From: [email protected]
Subject: [Haskell-beginners] liftM2, how does it work?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"

I am a bit puzzled by liftM2. I know what it does, but let's take a look at
 the implementation.
liftM2 :: (Monad m) => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r
 liftM2 f m1 m2 = do { x1 <- m1; x2 <- m2; return (f x1 x2) }
Which could be rewritten (I am a bit uncomfortable with the do notation):
m1 >>= \x1 ->
 m2 >>= \x2 ->
 return (f x1 x2)
so: liftM2 (+) [1,2] [3,4] = [4,5,5,6]
The instance of the List monad tells me that:
instance Monad [] where
 m >>= k = foldr ((++) . k) [] m
 m >> k = foldr ((++) . (\ _ -> k)) [] m
 return x = [x]
 fail _ = []
That foldr is just doing a concatMap, it seems to me.
 But I still don't get the | return (f x1 x2) | part.
 The first thing I thought was:
return ( (+) [1,2] [3,4] )
but of course this is not the IO monad, the bind operator does not just
 fetch stuff from outer space.

 What is there really 'inside' x1 and x2 when the return statement is
 evaluated, though?
I think I intuitively got it (x1 and x2 are bound to m1 and m2, every
 f x1 x2 expression is evaluated and then put together in a list), but
 it would be helpful to see what really happens to interiorise it.
I started from the first step
(>>=) m1 = foldr. ((++) . k) [] m1 -- partially applying m1
but cannot really proceed from there. Any help will be appreciated,
 if you think the question is not clear enough, do not hesitate to ask.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20120224/bf836e94/attachment-0001.htm>

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

Message: 2
Date: Fri, 24 Feb 2012 18:32:05 +0100
From: Vincent Ambo <[email protected]>
Subject: [Haskell-beginners] Text.JSON.JSON instance for
        Data.Text.Text
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=windows-1252

Hej,

I'm trying to create a JSON instance for Data.Text.Text. The CouchDB package 
uses Text.JSON, otherwise I would've considered using Aeson ?

This is what I've got so far: http://hpaste.org/64281

showJSON and readJSON work with this, but fromJSON and toJSON from 
Text.JSON.Generic don't. I'm not really sure how to 'tell' these functions how 
to use my instance.

Any ideas? :]

Vincent


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

Message: 3
Date: Fri, 24 Feb 2012 12:05:17 -0600
From: Joshua Weaver <[email protected]>
Subject: [Haskell-beginners] Example code on haskell.org
To: [email protected]
Message-ID:
        <caok1qvdzhklb91xrryhgbxued8pqnyr9p69xbfespstk7bv...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

FYI - Most of the links at
http://www.haskell.org/haskellwiki/Example_code return with a 403.

-- 

_josh



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

Message: 4
Date: Fri, 24 Feb 2012 15:11:42 -0500
From: Kyle Murphy <[email protected]>
Subject: Re: [Haskell-beginners] liftM2, how does it work?
To: [email protected]
Cc: [email protected]
Message-ID:
        <CA+y6JcwrHXJa=x5mcxxv0r6uzhyroci3ez+lprppe7xs0t7...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

It might be helpful to fully replace the >>= operators with their
definitions:

m1 >>= \x1 -> m2 >>= \x2 -> return (f x1 x2)
foldr ((++) . (\x1 -> m2 >>= \x2 -> return (f x1 x2))) [] m1
foldr ((++) . (\x1 -> foldr ((++) . (\x2 -> return (f x1 x2))) [] m2)) [] m1
foldr ((++) . (\x1 -> foldr ((++) . (\x2 -> [f x1 x2])) [] m2)) [] m1

which in your example yields:

foldr ((++) . (\x1 -> foldr ((++) . (\x2 -> [x1 + x2])) [] [3,4])) [] [1,2]

Does that make a bit more sense?

-R. Kyle Murphy
--
Curiosity was framed, Ignorance killed the cat.


On Fri, Feb 24, 2012 at 11:42, <[email protected]> wrote:

>  I am a bit puzzled by liftM2. I know what it does, but let's take a look
> at
> the implementation.
>
> liftM2  :: (Monad m) => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r
> liftM2 f m1 m2          = do { x1 <- m1; x2 <- m2; return (f x1 x2) }
>
> Which could be rewritten (I am a bit uncomfortable with the do notation):
>
> m1               >>= \x1 ->
> m2               >>= \x2 ->
> return (f x1 x2)
>
> so: liftM2 (+) [1,2] [3,4] = [4,5,5,6]
>
> The instance of the List monad tells me that:
>
> instance  Monad []  where
>     m >>= k   = foldr ((++) . k) [] m
>     m >> k    = foldr ((++) . (\ _ -> k)) [] m
>     return x  = [x]
>     fail _    = []
>
> That foldr is just doing a concatMap, it seems to me.
> But I still don't get the | return (f x1 x2) | part.
> The first thing I thought was:
>
> return ( (+) [1,2] [3,4] )
>
> but of course this is not the IO monad, the bind operator does not just
> fetch stuff from outer space.
>
> What is there really 'inside' x1 and x2 when the return statement is
> evaluated, though?
>
> I think I intuitively got it (x1 and x2 are bound to m1 and m2, every
> f x1 x2 expression is evaluated and then put together in a list), but
> it would be helpful to see what really happens to interiorise it.
>
> I started from the first step
>
> (>>=) m1 = foldr. ((++) . k) [] m1 -- partially applying m1
>
> but cannot really proceed from there. Any help will be appreciated,
> if you think the question is not clear enough, do not hesitate to ask.
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20120224/80b69382/attachment-0001.htm>

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

Message: 5
Date: Fri, 24 Feb 2012 22:16:55 +0100
From: Franco <[email protected]>
Subject: Re: [Haskell-beginners] liftM2, how does it work?
To: Kyle Murphy <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII

Very helpful, thanks! Let me point out, for future reference, that replacing 
>>= definition with

m1 >>= f = concatMap f m1

leads to an even simpler substitution (as far as you want to /understand/ it. I 
am pretty sure the actual implementation is more efficient)

-F

On Fri, 24 Feb 2012 15:11:42 -0500
Kyle Murphy <[email protected]> wrote:

> It might be helpful to fully replace the >>= operators with their
> definitions:
> 
> m1 >>= \x1 -> m2 >>= \x2 -> return (f x1 x2)
> foldr ((++) . (\x1 -> m2 >>= \x2 -> return (f x1 x2))) [] m1
> foldr ((++) . (\x1 -> foldr ((++) . (\x2 -> return (f x1 x2))) [] m2)) [] m1
> foldr ((++) . (\x1 -> foldr ((++) . (\x2 -> [f x1 x2])) [] m2)) [] m1
> 
> which in your example yields:
> 
> foldr ((++) . (\x1 -> foldr ((++) . (\x2 -> [x1 + x2])) [] [3,4])) [] [1,2]
> 
> Does that make a bit more sense?
> 
> -R. Kyle Murphy
> --
> Curiosity was framed, Ignorance killed the cat.
> 
> 
> On Fri, Feb 24, 2012 at 11:42, <[email protected]> wrote:
> 
> >  I am a bit puzzled by liftM2. I know what it does, but let's take a look
> > at
> > the implementation.
> >
> > liftM2  :: (Monad m) => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r
> > liftM2 f m1 m2          = do { x1 <- m1; x2 <- m2; return (f x1 x2) }
> >
> > Which could be rewritten (I am a bit uncomfortable with the do notation):
> >
> > m1               >>= \x1 ->
> > m2               >>= \x2 ->
> > return (f x1 x2)
> >
> > so: liftM2 (+) [1,2] [3,4] = [4,5,5,6]
> >
> > The instance of the List monad tells me that:
> >
> > instance  Monad []  where
> >     m >>= k   = foldr ((++) . k) [] m
> >     m >> k    = foldr ((++) . (\ _ -> k)) [] m
> >     return x  = [x]
> >     fail _    = []
> >
> > That foldr is just doing a concatMap, it seems to me.
> > But I still don't get the | return (f x1 x2) | part.
> > The first thing I thought was:
> >
> > return ( (+) [1,2] [3,4] )
> >
> > but of course this is not the IO monad, the bind operator does not just
> > fetch stuff from outer space.
> >
> > What is there really 'inside' x1 and x2 when the return statement is
> > evaluated, though?
> >
> > I think I intuitively got it (x1 and x2 are bound to m1 and m2, every
> > f x1 x2 expression is evaluated and then put together in a list), but
> > it would be helpful to see what really happens to interiorise it.
> >
> > I started from the first step
> >
> > (>>=) m1 = foldr. ((++) . k) [] m1 -- partially applying m1
> >
> > but cannot really proceed from there. Any help will be appreciated,
> > if you think the question is not clear enough, do not hesitate to ask.
> >
> > _______________________________________________
> > Beginners mailing list
> > [email protected]
> > http://www.haskell.org/mailman/listinfo/beginners
> >
> >


-- 
Franco <[email protected]>



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

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 44, Issue 24
*****************************************

Reply via email to