Consider the Object-Oriented Languages:
Most people coming from an object-oriented programming background, are familiar with the common and intuitive interfaces in various languages that capture the essence of Java's Collection
& List
interfaces. Collection
refers to a collection of objects which doesn't necessarily have an natural ordering/indexing. A List
is a collection which has a natural ordering/indexing. These interfaces abstract many library data-structures in Java, as do their equivalent interfaces in other languages, and an intimate understanding of these interfaces are required to work effectively with most library data-structures.
Transition to Haskell:
Haskell has a type-class system which acts on types analogously to interfaces on objects. Haskell seems to have a well designed type-class hierarchy with regard to Functors, Applicative, Monads, etc. when the type regard functionality. They obviously want correct and well-abstracted type-classes. Yet when you look at many Haskell's containers (List
,Map
,Sequence
,Set
,Vector
) they almost all have very similar (or identical) functions, yet aren't abstracted through type-classes.
Some Examples:
null
for testing "emptyness"length
/size
for element countelem
/member
for set inclusionempty
and/orsingleton
for default constructionunion
for set union(\\)
/diff
for set difference(!)
/(!!)
for unsafe indexing (partial function)(!?)
/lookup
for safe indexing (total function)
If I want to use any of the functions above, but I have imported two or more containers I have to start hiding functions from the imported modules, or explicitly import only the necessary functions from the modules, or qualifying the imported modules. But since all the functions provide the same logical functionality, it just seems like a hassle. If the functions were defined from type-classes, and not separately in each module, the compiler's type inference mechanics could resolve this. It would also make switching underlying containers simple as long as they shared the type-classes (ie: lets just use a Sequence
instead of List
for better random access efficiency).
Why doesn't Haskell have a Collection
and/or Indexable
type-class(es) to unify & generalize some of these functions?
isMember :: Ord k => k -> Set k -> Bool
vsisMember :: a -> [a] -> Bool
. Or indexing:at :: Int -> [a] -> Maybe a
vsat :: Unbox a => Int -> Vector a -> Maybe a
(for unboxed vectors). Other than that, I concur with Daniel, it's hard to answer in an objective way. If you can create your specific version ofCollection
, go for it, and add it to hackage. – Zetaelem
would actually be possible withConstraintKinds
. – leftaroundabout