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

Eric Rasmussen ericrasmussen at gmail.com
Thu Mar 28 05:22:55 CET 2013


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.


On Wed, Mar 27, 2013 at 1:16 PM, Eli Frey <eli.lee.frey at gmail.com> wrote:

> > 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.lee.frey at gmail.com> wrote:
>
>> > http://hackage.haskell.org/trac/ghc/wiki/OverloadedLists comes to mind.
>>
>> 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?
>>
>>
>> On Wed, Mar 27, 2013 at 12:56 PM, Nicolas Trangez <nicolas at incubaid.com>wrote:
>>
>>> 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
>>>
>>>
>>> _______________________________________________
>>> Haskell-Cafe mailing list
>>> Haskell-Cafe at haskell.org
>>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>>>
>>
>>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20130327/77a097bb/attachment.htm>


More information about the Haskell-Cafe mailing list