I'm still new to Haskell (learning it on and off). I'm wondering why Haskell doesn't have a literal Data.Map
constructor syntax, like the Map/Hash constructor syntax in Clojure or Ruby. Is there a reason? I thought that since Haskell does have a literal constructor syntax for Data.List
, there should be one for Data.Map
.
This question is not meant to be critical at all. I would just like to learn more about Haskell through the answers.
Unlike Clojure and Ruby, Haskell's finite maps are provided as libraries. This has tradeoffs: for example, as you noticed, there's no built-in syntax for finite maps; however, because it's a library, we can (and do) have many alternative implementations, and you as a programmer can choose the one that's most appropriate for your uses.
In addition to the answers already given (the "historical accident" one notwithstanding), I think there's also something to be said for the use of Data.Map
in Haskell compared to Hash
in Ruby or similar things; map-like objects in other languages tend to see a lot more use for general ad-hoc storage.
Whereas in Haskell you'd whip up a data
definition in no time at all, creating a class in other languages tends to be somewhat heavy-weight, and so we find that even for data with a well-known structure, we'll just use a Hash
or dict
or similar. The fact that we have a direct syntax for doing so makes it all the more attractive an option.
Contrast to Lisp: using MAKE-HASH-TABLE
and then repeatedly SETF
ing it is relatively annoying (similar to using Data.Map
), so everything gets thrown into nested lists, instead—because it's what's convenient.
Similarly, I'm happy that the most convenient choice for storing data is creating new types to suit, and then I leave Data.Map
to when I'm actually constructing a map or hash-table as an intrinsic component. There are a few cases where I think the syntax would be nice (usually just for smaller throw-away programs), but in general I don't miss it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With