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.  Syntactic sugar to insert into a Map? (martin)
   2. Re:  Syntactic sugar to insert into a Map? (Mateusz Kowalczyk)
   3. Re:  Syntactic sugar to insert into a Map? (martin)
   4. Re:  Syntactic sugar to insert into a Map? (Mateusz Kowalczyk)
   5. Re:  Syntactic sugar to insert into a Map? (martin)
   6. Re:  Syntactic sugar to insert into a Map? (Kim-Ee Yeoh)
   7. Re:  Syntactic sugar to insert into a Map? (Ozgur Akgun)


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

Message: 1
Date: Sun, 21 Jul 2013 14:36:36 +0200
From: martin <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] Syntactic sugar to insert into a Map?
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-15

Hello all,

What is the best way to enter several (key, value) pairs into a Map,
such that the code "looks nice", kind of like the do-notation with monads.

I suppose this question is not really specific to a Map. It is about
chaining functions with a type of a1 ->...-> an -> b -> b.

Is there any syntactic sugar available?
        



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

Message: 2
Date: Sun, 21 Jul 2013 14:08:13 +0100
From: Mateusz Kowalczyk <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Syntactic sugar to insert into a Map?
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 21/07/13 13:36, martin wrote:
> Hello all,
> 
> What is the best way to enter several (key, value) pairs into a
> Map, such that the code "looks nice", kind of like the do-notation
> with monads.
> 
> I suppose this question is not really specific to a Map. It is
> about chaining functions with a type of a1 ->...-> an -> b -> b.
> 
> Is there any syntactic sugar available? 
> 
> _______________________________________________ Beginners mailing
> list [email protected] 
> http://www.haskell.org/mailman/listinfo/beginners
> 
There's no sugar but that doesn't mean you have to have something that
doesn't look nice! Hackage tells me it's an instance of monoid.

*Main Data.Monoid Data.Map> singleton 1 2
fromList [(1,2)]
*Main Data.Monoid Data.Map> singleton 1 2 <> singleton 2 3
fromList [(1,2),(2,3)]
*Main Data.Monoid Data.Map> singleton 1 2 <> singleton 2 3 <>
singleton 4 3
fromList [(1,2),(2,3),(4,3)]

There's of course fromList
*Main Data.Monoid Data.Map> fromList [(1, 2), (3, 4)] <> fromList [(7,
2), (8, 4)]
fromList [(1,2),(3,4),(7,2),(8,4)]

*Main Data.Monoid Data.Map> fromList $ zip [1 .. 3] ['a' .. 'z']
fromList [(1,'a'),(2,'b'),(3,'c')]

I don't think that fromList looks nice enough, especially considering
that you can split it across many lines etc. It'd be easier to help if
you could be more specific about what you're looking for.

As per your chaining question, no, there's no general way to do this
for arbitrary `n' that I'm aware of (that doesn't involve some real
hackery which doesn't seem like what you're looking for)

- -- 
Mateusz K.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.20 (GNU/Linux)

iQIcBAEBAgAGBQJR6909AAoJEM1mucMq2pqX4AcP/AoCGqV5TO+SWQWlSYCtD21L
BbXMJY2ruwdfcuRm8o8IR3iuStVjErjtrqmeNschpbCyqWpwECliabpCCslQygpg
NpeTDhMW7rJSdj+7lPAnBtB/YFNYxsFi9RpZgJftUDPZ843urZArn/zmH5vDtLvH
iG9wpo/TXXOV6obZ04/HQeXAoPqlHQFOfG8cgmSBtYsCU/tyxI5tH4aw1K2QgsyI
fP7IK948wxY09mZQph8NESjOfsvmBa+3VJfFPz7x4+MbsShfYrf2ffA7RtAb+g4E
KTEjoMBSl++AG/B1b6ecGPdeCughjiAAKOPwKqwCyZpdNe3T21E74N3iCOuOHOk5
Pd/uYd/FSVx+M5lWuaHE54UE12yRMYh+MQ1L7QIoOU0CTvMOJ8u3iPa0oA/dBdeW
CCYzxszqXw6uCpZ/9/eZ01xne5YioCFnXZlIegijOhMKkzsvBCuL8WUlrIedd+3k
xWkg54fojqDvebGoaE3v19bswoBiJ7IZs4jScooxyEJU4/7kRBSZksilnpC6MkIz
+pq2owqUmNEXfAozG5vmmH2SZcOmmt1d7EV/c/IyiyAZZh+Zre2nhu2w20E0RWvE
sWBdefCxemdMSvXr7XfCUPsU21uUACKVgIaTg8PcddGG21wMM78qgDIc/F7RZIqx
rBylBbTtAjG0DbuvLLDi
=Aab2
-----END PGP SIGNATURE-----



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

Message: 3
Date: Sun, 21 Jul 2013 16:23:56 +0200
From: martin <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Syntactic sugar to insert into a Map?
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Am 07/21/2013 03:08 PM, schrieb Mateusz Kowalczyk:
> On 21/07/13 13:36, martin wrote:
>> Hello all,
> 
>> What is the best way to enter several (key, value) pairs into a 
>> Map, such that the code "looks nice", kind of like the
>> do-notation with monads.
> 
>> I suppose this question is not really specific to a Map. It is 
>> about chaining functions with a type of a1 ->...-> an -> b -> b.
> 
>> Is there any syntactic sugar available?
> 
>> _______________________________________________ Beginners
>> mailing list [email protected] 
>> http://www.haskell.org/mailman/listinfo/beginners
> 
> There's no sugar but that doesn't mean you have to have something
> that doesn't look nice! Hackage tells me it's an instance of
> monoid.
> 
> *Main Data.Monoid Data.Map> singleton 1 2 fromList [(1,2)] *Main
> Data.Monoid Data.Map> singleton 1 2 <> singleton 2 3 fromList
> [(1,2),(2,3)] *Main Data.Monoid Data.Map> singleton 1 2 <>
> singleton 2 3 <> singleton 4 3 fromList [(1,2),(2,3),(4,3)]
> 
> There's of course fromList *Main Data.Monoid Data.Map> fromList
> [(1, 2), (3, 4)] <> fromList [(7, 2), (8, 4)] fromList
> [(1,2),(3,4),(7,2),(8,4)]
> 
> *Main Data.Monoid Data.Map> fromList $ zip [1 .. 3] ['a' .. 'z'] 
> fromList [(1,'a'),(2,'b'),(3,'c')]
> 
> I don't think that fromList looks nice enough, especially
> considering that you can split it across many lines etc. It'd be
> easier to help if you could be more specific about what you're
> looking for.

I just want to insert several key/value pairs, something like

Map.empty
insert key1 val1
insert key2 val2
return theMap

I know I can achieve this with dollars and by reversing the order,
such  that Map.empty is at the end. But I don't think it is pretty.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iEYEARECAAYFAlHr7u4ACgkQKkUHbleEZGW2DgCgmInJcFCcRCNpW/rdvq1X5Q8W
NJwAnA7SjDs+nGxOv77VZblb7Zge+d0x
=0G+B
-----END PGP SIGNATURE-----



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

Message: 4
Date: Sun, 21 Jul 2013 16:14:07 +0100
From: Mateusz Kowalczyk <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Syntactic sugar to insert into a Map?
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 21/07/13 15:23, martin wrote:
> Am 07/21/2013 03:08 PM, schrieb Mateusz Kowalczyk:
>> On 21/07/13 13:36, martin wrote:
>>> Hello all,
> 
>>> What is the best way to enter several (key, value) pairs into a
>>>  Map, such that the code "looks nice", kind of like the 
>>> do-notation with monads.
> 
>>> I suppose this question is not really specific to a Map. It is
>>>  about chaining functions with a type of a1 ->...-> an -> b ->
>>> b.
> 
>>> Is there any syntactic sugar available?
> 
>>> _______________________________________________ Beginners 
>>> mailing list [email protected] 
>>> http://www.haskell.org/mailman/listinfo/beginners
> 
>> There's no sugar but that doesn't mean you have to have
>> something that doesn't look nice! Hackage tells me it's an
>> instance of monoid.
> 
>> *Main Data.Monoid Data.Map> singleton 1 2 fromList [(1,2)] *Main 
>> Data.Monoid Data.Map> singleton 1 2 <> singleton 2 3 fromList 
>> [(1,2),(2,3)] *Main Data.Monoid Data.Map> singleton 1 2 <> 
>> singleton 2 3 <> singleton 4 3 fromList [(1,2),(2,3),(4,3)]
> 
>> There's of course fromList *Main Data.Monoid Data.Map> fromList 
>> [(1, 2), (3, 4)] <> fromList [(7, 2), (8, 4)] fromList 
>> [(1,2),(3,4),(7,2),(8,4)]
> 
>> *Main Data.Monoid Data.Map> fromList $ zip [1 .. 3] ['a' .. 'z']
>>  fromList [(1,'a'),(2,'b'),(3,'c')]
> 
>> I don't think that fromList looks nice enough, especially 
>> considering that you can split it across many lines etc. It'd be 
>> easier to help if you could be more specific about what you're 
>> looking for.
> 
> I just want to insert several key/value pairs, something like
> 
> Map.empty insert key1 val1 insert key2 val2 return theMap
> 
> I know I can achieve this with dollars and by reversing the order, 
> such  that Map.empty is at the end. But I don't think it is
> pretty.
> 
> 
> _______________________________________________ Beginners mailing
> list [email protected] 
> http://www.haskell.org/mailman/listinfo/beginners
> 
Map is not a monad so you can't use do. `do' is just sugar for >>= so
if you don't have a monad, can't use it. You could try running it
inside of the State monad but I think you'll find that it's a lot of
hassle to do and is just not worth it.
- -- 
Mateusz K.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.20 (GNU/Linux)

iQIcBAEBAgAGBQJR6/q/AAoJEM1mucMq2pqXqSwP/0GBUd28y34DPystvY8+Sgbs
363IPm1QDTXrX7gkc3OTelOPoqgZmzlF1uKu3OGcjlD9mxqiyTv4QPXSjieslpe3
QSE6O/5BqQYokbBfmVyEuCHamr4z/ta53F/W9IA7ElIvXb6cMupy78VB+Qnybsss
G3JOZeDCG5CMuBSmaDpk3iN8LGEQxbDgriEYraC1N5GFv2dQ+5vcRHn4XD8C8yH9
2WAAlIHncKTPNX7hWnuiIYzj2fwhbtBa1oImtufEsAjKfmQHoorG7ZBG0SSKzk9X
lxpxLNW/Rh0L3EYIprUl2PVCjC/r3FRT2v+TtToLsHuFt8RYM0bCMk/gM8KhjKxg
LXNUKlCiKdAK9vFF8/5NHGD7G8yAueqR5tY6YpLoL21NL3SLuFM2DFFT5nQNtHO4
awa4ie1e0rA1iRcho/r/TH1Pr3i/AN1uNyQCDyEQFHloiyv2x8dpEDxMnPfIJe44
teEapLwWy/y0qpRuXn2wiyrQsVmr+9F1In/DfE/KR9gYsbnvGZPIGiZ9JWfgQ5z1
3FRVW5gVDyt/mHKXxt0c9CPI3lA0cuLTrUL6UrBRzfajSCIzvbVW/MpzJ4h/5VDj
Tf+pggHVQbiLnGCsGwVtj1p1CfDUp1yqF3SmoDaeODIY0qJ+gHL9rR49VVRB1eo9
MHippyvD6ZiyqwdxfAjH
=bS97
-----END PGP SIGNATURE-----



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

Message: 5
Date: Sun, 21 Jul 2013 19:06:29 +0200
From: martin <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Syntactic sugar to insert into a Map?
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Am 07/21/2013 05:14 PM, schrieb Mateusz Kowalczyk:

>>> I don't think that fromList looks nice enough, especially 
>>> considering that you can split it across many lines etc.

It seems like fromList is as pretty as it gets. Actually it is not
that bad.

Thanks for the hint.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iEYEARECAAYFAlHsFRAACgkQKkUHbleEZGWxIQCeLlTwbtvjcixia0PYg50JRSNr
rMQAn3j2CmOX7fx0vrSmbGO3w2mp5/6l
=gI1P
-----END PGP SIGNATURE-----



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

Message: 6
Date: Mon, 22 Jul 2013 05:44:02 +0700
From: Kim-Ee Yeoh <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Syntactic sugar to insert into a Map?
Message-ID:
        <CAPY+ZdRdQnLHxjSMD7Vir5VfXoBRwbrp++Dnro-Wef_D=eq...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

On Mon, Jul 22, 2013 at 12:06 AM, martin <[email protected]> wrote:

> It seems like fromList is as pretty as it gets. Actually it is not
> that bad.
>

Agreed. What you might want to look  into is the difference between
/expressions/ and imperative statements, also known as /commands/.

The effectful nature of commands is typically modeled in Haskell using
monads, which may explain your references to do-notation and chaining
functions.

The mapping operations you're asking about can be easily /described/ using
expressions, so monads don't need to enter the picture. Minimize effects
whenever possible.

E.g.

> m0 = fromList [(key1,val1),(key2,val2)]
> m1 = m0 <> fromList [(key3,val3),(key4,val4)]

-- Kim-Ee
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130722/67fc62a0/attachment-0001.html>

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

Message: 7
Date: Mon, 22 Jul 2013 10:06:20 +0300
From: Ozgur Akgun <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Syntactic sugar to insert into a Map?
Message-ID:
        <calzazpdhtwzan5otjrugf1n3almdkpuq3gu99p4m0rv33yf...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Hi.

On 21 July 2013 17:23, martin <[email protected]> wrote:

> I just want to insert several key/value pairs, something like
>
> Map.empty
> insert key1 val1
> insert key2 val2
> return theMap
>

If you really want this syntax, what about using the writer monad:

import Control.Monad.Writer
import qualified Data.Map as M

m = execWriter $ do
    insert 1 "foo"
    insert 2 "bar"
    where insert k v = tell (M.singleton k v)
-- m = fromList [(1,"foo"),(2,"bar")]


Hope this helps,
Ozgur
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130722/70430097/attachment-0001.html>

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

Subject: Digest Footer

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


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

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

Reply via email to