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.  Python's collections.defaultdict(list) in        Haskell?
      (Dan Stromberg)
   2. Re:  Python's collections.defaultdict(list) in    Haskell?
      (Sylvain Henry)
   3. Re:  Python's collections.defaultdict(list) in    Haskell?
      (Dan Stromberg)


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

Message: 1
Date: Mon, 9 Nov 2015 18:07:49 -0800
From: Dan Stromberg <strom...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: [Haskell-beginners] Python's collections.defaultdict(list) in
        Haskell?
Message-ID:
        <caovkw57++6mtdw4cuq2d+t7g33_s2abwxt7nwkddddrtokm...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

I'm spending a little time here and there to learn some Haskell.  I'm
coming from a chiefly Python/C/bash background.

I want to build a Data.Map where the keys are strings, and the values are
lists of strings.

In Python, collections.defaultdict(list) makes this pretty straightforward.
It gives a hash table ("dict") that has values that default to an empty
list, since list() produces an empty list. More info here:
https://docs.python.org/3/library/collections.html#collections.defaultdict

Is there an equivalent in Haskell?

Thanks!

-- 
Dan Stromberg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20151109/16650a46/attachment-0001.html>

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

Message: 2
Date: Tue, 10 Nov 2015 04:45:54 +0100
From: Sylvain Henry <hsy...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Python's
        collections.defaultdict(list) in        Haskell?
Message-ID:
        <capmptcu784qmwqbqvx-wm2numeogxwyo7vhrgazbvyp5xpp...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

import qualified Data.Map as Map

-- if your keys are unique
let xs = [("Item0", ["a","b","c"]), ("Item1", ["x","y"]), ("Item2",
["abc","def"])]
Map.fromList xs

-- if you want to combine values for keys that are equal
let xs = [("Item0", ["a","b","c"]), ("Item1", ["x","y"]), ("Item0",
["abc","def"])]
Map.fromListWith (++) xs

--
Sylvain


2015-11-10 3:07 GMT+01:00 Dan Stromberg <strom...@gmail.com>:

>
> I'm spending a little time here and there to learn some Haskell.  I'm
> coming from a chiefly Python/C/bash background.
>
> I want to build a Data.Map where the keys are strings, and the values are
> lists of strings.
>
> In Python, collections.defaultdict(list) makes this pretty
> straightforward. It gives a hash table ("dict") that has values that
> default to an empty list, since list() produces an empty list. More info
> here:
> https://docs.python.org/3/library/collections.html#collections.defaultdict
>
> Is there an equivalent in Haskell?
>
> Thanks!
>
> --
> Dan Stromberg
>
> _______________________________________________
> 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/20151110/2b52381a/attachment-0001.html>

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

Message: 3
Date: Mon, 9 Nov 2015 21:23:27 -0800
From: Dan Stromberg <strom...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Python's
        collections.defaultdict(list) in        Haskell?
Message-ID:
        <CAOvKW54J6S9atCjE-r04ANvu5gTWG5RVT75gC-_97ibt=n7...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

These are some good leads.

I'll be adding values one at a time, and yes, my keys aren't necessarily
unique.

Is there a way of cons'ing on the single values one at a time, that will
avoid the slowness of ++ ?

Thanks.

On Mon, Nov 9, 2015 at 7:45 PM, Sylvain Henry <hsy...@gmail.com> wrote:

> import qualified Data.Map as Map
>
> -- if your keys are unique
> let xs = [("Item0", ["a","b","c"]), ("Item1", ["x","y"]), ("Item2",
> ["abc","def"])]
> Map.fromList xs
>
> -- if you want to combine values for keys that are equal
> let xs = [("Item0", ["a","b","c"]), ("Item1", ["x","y"]), ("Item0",
> ["abc","def"])]
> Map.fromListWith (++) xs
>
> --
> Sylvain
>
>
> 2015-11-10 3:07 GMT+01:00 Dan Stromberg <strom...@gmail.com>:
>
>>
>> I'm spending a little time here and there to learn some Haskell.  I'm
>> coming from a chiefly Python/C/bash background.
>>
>> I want to build a Data.Map where the keys are strings, and the values are
>> lists of strings.
>>
>> In Python, collections.defaultdict(list) makes this pretty
>> straightforward. It gives a hash table ("dict") that has values that
>> default to an empty list, since list() produces an empty list. More info
>> here:
>> https://docs.python.org/3/library/collections.html#collections.defaultdict
>>
>> Is there an equivalent in Haskell?
>>
>> Thanks!
>>
>> --
>> Dan Stromberg
>>
>> _______________________________________________
>> 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
>
>


-- 
Dan Stromberg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20151109/8a39d11f/attachment-0001.html>

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

Subject: Digest Footer

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


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

End of Beginners Digest, Vol 89, Issue 14
*****************************************

Reply via email to