[Haskell-cafe] [extension]syntactic sugar for maps

21 views
Skip to first unread message

Răzvan Rotaru

unread,
Mar 27, 2013, 3:30:15 PM3/27/13
to haskel...@haskell.org
Hi,

I am terribly missing some syntactic sugar for maps (associative data structures) in Haskell. I find myself using them more than any other data structure, and I think there is no big deal in adding some sugar for this to the language. I could not find out whether such an extension is beeing discussed. If not, I would like to propose and extension. Any help and suggestions are very welcome here. Thanks.

Also related to the topic:

1/ Is there a list of proposals for extensions to Haskell that has currently been accepted in the new standard? I have not found one on Haskell' (http://hackage.haskell.org/trac/haskell-prime/query?status=new&status=assigned&status=reopened&group=state), but it looks outdated. No mentions to the new standard!

2/ I have seen somewhere a statement that a new language standard will be published yearly. Didn't happen until now. Is there even a new standard on the way?

Cheers,
Răzvan

Ivan Lazar Miljenovic

unread,
Mar 27, 2013, 3:48:34 PM3/27/13
to Răzvan Rotaru, haskel...@haskell.org
On 28 March 2013 06:30, Răzvan Rotaru <razvan...@gmail.com> wrote:
> Hi,
>
> I am terribly missing some syntactic sugar for maps (associative data
> structures) in Haskell. I find myself using them more than any other data
> structure, and I think there is no big deal in adding some sugar for this to
> the language. I could not find out whether such an extension is beeing
> discussed. If not, I would like to propose and extension. Any help and
> suggestions are very welcome here. Thanks.

What kind of syntactic sugar are you wanting?

>
> Also related to the topic:
>
> 1/ Is there a list of proposals for extensions to Haskell that has currently
> been accepted in the new standard? I have not found one on Haskell'
> (http://hackage.haskell.org/trac/haskell-prime/query?status=new&status=assigned&status=reopened&group=state),
> but it looks outdated. No mentions to the new standard!
>
> 2/ I have seen somewhere a statement that a new language standard will be
> published yearly. Didn't happen until now. Is there even a new standard on
> the way?
>
> Cheers,
> Răzvan
>

> _______________________________________________
> Haskell-Cafe mailing list
> Haskel...@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>

--
Ivan Lazar Miljenovic
Ivan.Mi...@gmail.com
http://IvanMiljenovic.wordpress.com

_______________________________________________
Haskell-Cafe mailing list
Haskel...@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Tikhon Jelvis

unread,
Mar 27, 2013, 3:54:29 PM3/27/13
to Răzvan Rotaru, haskell-cafe

I'm genuinely curious as to how you use maps. I've found I use them far less in Haskell than in any other language: I only use them in select circumstances. And most of those uses would not benefit from a mayo literal.

I suspect that many of the uses of map literals are better replaced with something like a record. This has the advantage of being static and more type safe. However, this is only based on my own use cases, so it's hard to generalize.

Nicolas Trangez

unread,
Mar 27, 2013, 3:56:18 PM3/27/13
to haskel...@haskell.org
On Wed, 2013-03-27 at 21:30 +0200, Răzvan Rotaru wrote:
> I am terribly missing some syntactic sugar for maps (associative data
> structures) in Haskell. I find myself using them more than any other
> data
> structure, and I think there is no big deal in adding some sugar for
> this
> to the language. I could not find out whether such an extension is
> beeing
> discussed. If not, I would like to propose and extension. Any help and
> suggestions are very welcome here. Thanks.

http://hackage.haskell.org/trac/ghc/wiki/OverloadedLists comes to mind.

Nicolas

Răzvan Rotaru

unread,
Mar 27, 2013, 3:56:45 PM3/27/13
to Ivan Lazar Miljenovic, haskel...@haskell.org
Sorry, I forgot to explain (probably because I'm too used to it). I am referring to a syntax for easy creation of maps. Something equivalent to lists:

to build a list: [ 1, 2, 3]
to build a map; { 1, "one", 2, "two", 3, "three"}

Without it I am always forced to use fromList.

Răzvan

Eli Frey

unread,
Mar 27, 2013, 4:16:09 PM3/27/13
to Haskell-Cafe
> Sorry, I forgot to explain (probably because I'm too used to it). I am referring to a syntax for easy creation of maps. Something equivalent to lists:
>
> to build a list: [ 1, 2, 3]
> to build a map; { 1, "one", 2, "two", 3, "three"}
>
> Without it I am always forced to use fromList.

This looks like something to use records for, or in any case something where association list performance is not an issue.

If you just want to store some configuration-like structure and pass it around, a record is great for this.  You might find where in other languages you would simply leave a key "null", in Haskell you can just fill it with a Nothing.

Maps (hash or binary-tree) really pay off when they are filled dynamically with massive numbers of associations.  I find when I am ending up in this scenario, I am generating my map programatically, not writing it as a literal.

Sometimes people even write maps simply as functions and not even as a data-structure.

> myMap char = case char of
>     'a' -> 1
>     'b' -> 2
>     'c' -> 3

Perhaps you could describe a situation you are in where you are wanting this, and we could see if there is something you can do already that is satisfying and solves your problem.



On Wed, Mar 27, 2013 at 12:59 PM, Eli Frey <eli.le...@gmail.com> wrote:
This assumes you can turn ANY list into a thing.  Maps only make sense to be constructed from association list.  If I've got a [Char], how do I make a map form it?

Eric Rasmussen

unread,
Mar 28, 2013, 12:22:55 AM3/28/13
to Haskell-Cafe
I agree that fromList or pattern matching at the function or case level are readable. We probably don't need new sugar. For what it's worth, in scala you can use "->" to construct tuples, so you'll sometimes see maps created like this:

Map(1 -> "one", 2 -> "two", 3 -> "foo")

You can always do something similar in haskell (keeping in mind that "->" is reserved):

import qualified Data.Map as Map

(-->) = (,)

makeMap = Map.fromList

myMap = makeMap [ 1 --> "one"
                , 2 --> "two"
                , 3 --> "foo"
                ]


Of course, it's not idiomatic and won't be immediately obvious to readers that you are constructing tuples. However, if you find it easier to read and need to write a lot of map literals in your code, it may be worth coming up with a couple of aliases similar to those.
Reply all
Reply to author
Forward
0 new messages