[Haskell-beginners] Python's collections.defaultdict(list) in Haskell?
Sylvain Henry
hsyl20 at gmail.com
Tue Nov 10 03:45:54 UTC 2015
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 <strombrg at 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 at 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.html>
More information about the Beginners
mailing list